Java程序成对反转链表
时间:2020-02-23 14:35:08 来源:igfitidea点击:
在本教程中,我们将看到如何成对地反转链表。
可以有两个解决方案,用于成对反转链接的列表
- 迭代
- 递归
迭代:
这将是:
public static Node reverseLinkedListInPairItr(Node head) { Node current=head; Node temp=null; Node newHead =null; while (current != null && current.next != null) { if (temp != null) { //This is important step temp.next.next = current.next; } temp=current.next; current.next=temp.next; temp.next=current; if (newHead == null) newHead = temp; current=current.next; } return newHead; }
解释:
借助简单的例子的帮助来理解:让我们说LinkedList是5-> 6 - > 7 - > 1如果你仔细观察,下面的步骤只是在第一次迭代中倒转到6-> 5-> 7-> 1的链接(交换节点6和5)然后前进到下一个对(节点7和1)
//Node 6 is temp node temp=current.next; //putting link between 5->7 current.next=temp.next; //putting link between 6->5 temp.next=current; //6 becomes head node here
我们必须想知道我们为什么要付出以下代码:
if (temp != null) { //This is important step temp.next.next = current.next; }
在交换两个节点之前,我们需要正确链接节点。
当我们交换7和1时,5-> 7之间的链接将被损坏,我们需要将节点5连接到节点1.根据上面的代码,在第一次迭代(交换节点6和5)中的临时将为空,但是当电流时节点是7,temp将是6,在这里我们将节点链接到6-> 5-> 1-> 7以下
Java计划为此:
package org.igi.theitroad; public class ReverseLinkedListInPair{ private Node head; private static class Node { private int value; private Node next; Node(int value) { this.value = value; } } public void addToTheLast(Node node) { if (head == null) { head = node; } else { Node temp = head; while (temp.next != null) temp = temp.next; temp.next = node; } } public void printList(Node head) { Node temp = head; while (temp != null) { System.out.format("%d ", temp.value); temp = temp.next; } System.out.println(); } //Reverse linked list in pair public static Node reverseLinkedListInPairs(Node head) { Node current=head; Node temp=null; Node newHead =null; while (current != null && current.next != null) { if (temp != null) { //This is important step temp.next.next = current.next; } temp=current.next; current.next=temp.next; temp.next=current; if (newHead == null) newHead = temp; current=current.next; } return newHead; } public static void main(String[] args) { ReverseLinkedListInPair list = new ReverseLinkedListInPair(); //Creating a linked list Node head=new Node(5); list.addToTheLast(head); list.addToTheLast(new Node(6)); list.addToTheLast(new Node(7)); list.addToTheLast(new Node(1)); list.addToTheLast(new Node(2)); list.addToTheLast(new Node(8)); list.printList(head); //Reversing LinkedList in pairs Node result=reverseLinkedListInPairs(head); System.out.println("After reversing in pair"); list.printList(result); } }
运行上面的程序,我们将获取以下输出:
5 6 7 1 2 8 After reversing 6 5 1 7 8 2
递归:
基本情况:基本情况为此为null为null或者node.next为null
对于递归解决方案,将上述程序的ReverselinkedListinapa替换为以下函数
public static Node reverseLinkedListInPairs(Node head) { if (head == null || head.next == null) { return head; } //Lets take example of 5->6->7 //Here head node is 5 //Storing 6 in temp node, it will become our new head Node temp=head.next; //Putting link between 5->7 head.next=temp.next; //putting link between 6->5 temp.next=head; //recursively calling the function for node 7 head.next=reverseLinkedListInPairRec(head.next); //returning new head return temp; }