反向链接 反向代理

Reversing a Linked List is an interesting problem in data structure and algorithms. In this tutorial, we’ll be discussing the various algorithms to reverse a Linked List and then implement them using Java.

反向链接列表是数据结构和算法中一个有趣的问题。 在本教程中,我们将讨论各种算法来反向链接列表 ,然后使用Java实现它们。

反向链接列表 (Reverse a Linked List)

LinkedList is a data structure which stores the data in a linear way. Though not in a contiguous way.

LinkedList是一种数据结构,它以线性方式存储数据。 虽然不是连续的。

Every element of a LinkedList contains a data part and an address to the next element of the LinkedList.

LinkedList的每个元素都包含一个数据部分和LinkedList的下一个元素的地址。

LinkedList elements are popularly known as nodes.

LinkedList元素通常称为节点。

Doubly LinkedList store two addresses. The address for the previous element and next element.
双向LinkedList存储两个地址。 上一个元素和下一个元素的地址。

In order to reverse a LinkedList in place, we need to reverse the pointers such that the next element now points to the previous element.

为了使LinkedList反向,我们需要反向指针,以便下一个元素现在指向上一个元素。

Following is the input and output.

以下是输入和输出。

Input

输入项

Output

输出量

The head of the LinkedList is the first node. No other element has the address stored for this.

LinkedList的头是第一个节点。 没有其他元素为此存储地址。

The tail of the LinkedList is the last node. The next address stored in this node is null.

LinkedList的尾部是最后一个节点。 存储在此节点中的下一个地址为null

We can reverse a LinkedList such that the head and tail also get changed using:

我们可以反转LinkedList,使头部和尾部也可以使用以下方式更改:

  • Iterative Approach迭代法
  • Recursive Approach递归方法

反向链接列表的迭代方法 (Iterative Approach to Reverse a Linked List)

To reverse a LinkedList iteratively, we need to store the references of the next and previous elements, so that they don’t get lost when we swap the memory address pointers to the next element in the LinkedList.

要迭代地反转LinkedList,我们需要存储下一个和上一个元素的引用,以便在将内存地址指针交换到LinkedList中的下一个元素时,它们不会丢失。

Following illustration demonstrates how we will reverse our LinkedList by changing the references.

下图说明了如何通过更改引用来反转LinkedList。

Create 3 instances: current, next, previous.

创建3个实例:当前,下一个,上一个。

Loop the following till current is NOT null:

循环以下,直到current不为空:

  • Save the next Node of the current element in the next pointer.将当前元素的下一个节点保存在下一个指针中。
  • Set the next of the current Node to the previous. This is the MVP line.将current节点的下一个设置为上previous 。 这是MVP行。
  • Shift previous to current.从上一个移到当前。
  • Shift the current element to next.将当前元素移到下一个。

In the end, since the current has gone one place ahead of the last element, we need to set the head to the last element we reached. This is available in previous.
Set head to previous. Thus we have our new head of the LinkedList which is the last element of the older LinkedList.

最后,由于电流已经比最后一个元素领先一个位置,因此我们需要将head设置为到达的最后一个元素。 这是以前可用的。
前往上一个。 因此,我们有了LinkedList的新头,它是旧LinkedList的最后一个元素。

Here is a very simple implementation of LinkedList. Note that this is not a production-ready implementation and we have kept it simple so that our focus remains on the algorithm to reverse the Linked List.

这是LinkedList的非常简单的实现。 请注意,这不是可用于生产的实现,因此我们将其保持简单,因此我们将重点放在反转链表的算法上。

package com.journaldev.linkedlist.reverse;public class MyLinkedList {public Node head;public static class Node {Node next;Object data;Node(Object data) {this.data = data;next = null;}}
}

The Java Program to reverse a Linked List iteratively and printing its elements is given below:

下面给出了Java程序以迭代方式反向链接列表并打印其元素的情况:

package com.journaldev.linkedlist.reverse;import com.journaldev.linkedlist.reverse.MyLinkedList.Node;public class ReverseLinkedList {public static void main(String[] args) {MyLinkedList myLinkedList = new MyLinkedList();myLinkedList.head = new Node(1);myLinkedList.head.next = new Node(2);myLinkedList.head.next.next = new Node(3);printLinkedList(myLinkedList);reverseLinkedList(myLinkedList);printLinkedList(myLinkedList);}public static void printLinkedList(MyLinkedList linkedList) {Node h = linkedList.head;while (linkedList.head != null) {System.out.print(linkedList.head.data + " ");linkedList.head = linkedList.head.next;}System.out.println();linkedList.head = h;}public static void reverseLinkedList(MyLinkedList linkedList) {Node previous = null;Node current = linkedList.head;Node next;while (current != null) {next = current.next;current.next = previous;previous = current;current = next;}linkedList.head = previous;}}

Output:

输出:

1 2 3
3 2 1

递归反向链接列表 (Reverse a Linked List Recursively)

To reverse a LinkedList recursively we need to divide the LinkedList into two parts: head and remaining.
Head points to the first element initially. Remaining points to the next element from the head.

要递归地反转LinkedList,我们需要将LinkedList分为两部分:头部分和剩余部分。
Head最初指向第一个元素。 其余部分指向头部的下一个元素。

We traverse the LinkedList recursively until the second last element.
Once we’ve reached the last element set that as the head.
From there we do the following until we reach the start of the LinkedList.

我们以递归方式遍历LinkedList,直到倒数第二个元素为止。
一旦到达最后一个元素,就将其设置为头部。
从那里开始执行以下操作,直到到达LinkedList的开始。

node.next.next = node;
node.next = null;

node.next.next = node;
node.next = null;

To not lose a track of the original head, we’ll save a copy of the head instance.

为了不丢失原始磁头,我们将保存磁头实例的副本。

Following is the illustration of the above procedure.

以下是上述过程的说明。

The Java program to reverse a LinkedList recursively is:

递归地反转LinkedList的Java程序是:

public static Node recursiveReverse(Node head) {Node first;if (head==null || head.next == null)return head;first = recursiveReverse(head.next);head.next.next = head;head.next = null;return first;
}

We just pass the head.next in the recursive call in order to go to the end of the LinkedList.

我们只需在递归调用中传递head.next即可到达LinkedList的末尾。

Once we reach the end, we set the second last element as the next of the last element and set the next pointer of second last element to NULL. The last element will be marked as the new head.

一旦到达末尾,就将倒数第二个元素设置为倒数第二个元素,并将倒数第二个元素的下一个指针设置为NULL。 最后一个元素将被标记为新头。

Use the following code to reverse the linked list using the recursive approach.

使用以下代码通过递归方法来反向链接列表。

myLinkedList.head = recursiveReverse(myLinkedList.head);

The output will remain as the previous iterative approach.

输出将保留为以前的迭代方法。

反向链接列表的时空复杂性 (Space Time Complexity of Reversing a Linked List)


Space Complexity – O(1)
空间复杂度– O(1)
GitHub Repository.GitHub存储库中检出完整的代码以及更多DS和算法示例。

翻译自: https://www.journaldev.com/23035/reverse-a-linked-list

反向链接 反向代理

反向链接 反向代理_反向链接列表相关推荐

  1. 微信链接修改图片_微信链接修改图标

    自定义链接是什么?微信链接修改图片_微信链接修改图标 自定义微信分享链接是指将一条网页链接通过微信接口生成一张卡片,并且该卡片的标题,内容和图片都可以自己编辑.如下图效果 ● 未自定义的网页链接 ● ...

  2. apache 反向代理_反向代理?听起来有点东西 ——Nginx学习笔记

    反向代理是个什么东西? 外部网络用户向内部服务器发出请求,并将这些请求转发给内部网络上的服务器,然后将从内部服务器上得到的响应返回给Internet上请求连接的客户:执行反向代理服务的服务器称为反向代 ...

  3. html制作花样链接卡页面_花样链接卡.html

    花样链接卡 div{ border: solid red 2px; width: 300px; height: 300px; padding: 10px; } a { color: white; di ...

  4. apache 反向代理_通过 Apache 与 Nginx 配置 AJP 配置反向代理

    ↑ 点击上面 "时代Java"关注我们, 关注新技术,学习新知识! 前言 目前,随着公有云的出现,一些大型的服务提供商将很多的基础服务以公有云的形式发布出来,而企业则可以使用这些基 ...

  5. 什么是反向链接,如何增加反向链接的数量

    什么是反向链接? 反向链接其实就是在目标文档内部进行声明.换言之,常规链接在文档 A 中标明"指向文档 B",而反向链接则在文档 B 中要求"使文档 A 指向我" ...

  6. fikker反向代理服务器做网站缓存加速时链接会转到源端口的问题

    2019独角兽企业重金招聘Python工程师标准>>> fikker反向代理服务器做网站缓存加速时链接会转到源端口的问题 最近在使用fikker反向代理服务器的过程中有人反映访问链接 ...

  7. python内网穿透 开源_反向代理和内网穿透

    概念 反向代理看上去看深奥,其实不然,只是因为汉语言文化的差异导致它看上去深奥.一般反派感觉都比较厉害和神秘. 要理解反向代理,我们就不得不说一下正向代理. 正向代理 (Forward Proxy) ...

  8. 构建Squid代理服务器-传统代理、透明代理、反向代理

    Squid是Linux系统中最常用的一款开源代理服务软件,主要提供缓存加速和应用层过滤控制的功能,可以很好的实现HTTP.FTP.DNS查询以及SSL等应用的缓存代理. 正向代理: 根据实现的方式不同 ...

  9. [转]nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件

    文章目录 前言 一.nginx简介 1. 什么是 nginx 和可以做什么事情 2.Nginx 作为 web 服务器 3. 正向代理 4. 反向代理 5. 负载均衡 6.动静分离 二.Nginx 的安 ...

最新文章

  1. 24本实体书包邮免费送!
  2. SAP系统如何快速上手?
  3. CVE-2017-0004相关lsass拒绝服务漏洞杂谈
  4. 12/100. Diameter of Binary Tree
  5. 【算法分析与设计】证明插入排序的正确性
  6. 文字描边_巧用Illustrator“3D”和“凸出和斜角”功能,制作炫酷立体文字
  7. centos 6.7 mysql_CentOS6.7 64位环境下安装部署MySQL-5.7.13
  8. CentOS 6.5设置静态IP教程 并且可以ping通
  9. 计算机二级通关宝典-C语言篇
  10. google地图通过经纬度查询位置
  11. highchart图表drilldown钻取功能及event点击事件添加(1)
  12. chrome下载速度慢,手把手教学
  13. win10快捷方式去箭头(win10快捷方式去箭头副作用)
  14. 全球与中国云计算数据中心IT资产处置(ITAD)市场深度研究分析报告
  15. 简易串口助手通信(齐全) 可实现ASII和十六进制发送指令 并显示
  16. Android开发技术框架和编码规范
  17. 操作符 算数操作符
  18. matlab可以仿真特殊电机,基于Matlab的异步电动机矢量控制系统的仿真研究
  19. Chomp game博弈游戏
  20. 香港为区块链专业人士降低移民门槛

热门文章

  1. javascript 连续滚动
  2. 在 Linux 上部署 Django 应用,nginx+gunicorn+supervisor
  3. [转载] python 装饰器
  4. [转载] Python从字符串中删除字符
  5. mysql中防止sql注入
  6. [转载]生活在 Emacs 中
  7. Springboot 使用wangEditor3.0上传图片
  8. item 12: 把重写函数声明为“override”的
  9. 汇编3-返回以及优化
  10. linux centos 系统php支持jpeg的安装方法