Created
February 22, 2019 14:50
-
-
Save mikeyang01/2e33bb2411d53beae4898ac521951950 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LinkedList_reverse { | |
public static void main(String[] args) { | |
Node head = new Node(0); | |
Node node1 = new Node(1); | |
Node node2 = new Node(2); | |
Node node3 = new Node(3); | |
head.setNext(node1); | |
node1.setNext(node2); | |
node2.setNext(node3); | |
// 打印反转前的链表 | |
Node h = head; | |
while (null != h) { | |
System.out.print(h.getData() + " "); | |
h = h.getNext(); | |
} | |
// 调用反转方法 | |
head = Reverse1(head); | |
System.out.println("\n**************************"); | |
// 打印反转后的结果 | |
while (null != head) { | |
System.out.print(head.getData() + " "); | |
head = head.getNext(); | |
} | |
} | |
/** | |
* 递归,在反转当前节点之前先反转后续节点 | |
*/ | |
public static Node Reverse1(Node head) { | |
// head看作是前一结点,head.getNext()是当前结点,reHead是反转后新链表的头结点 | |
if (head == null || head.getNext() == null) { | |
return head;// 若为空链或者当前结点在尾结点,则直接还回 | |
} | |
Node reHead = Reverse1(head.getNext());// 先反转后续节点head.getNext() | |
head.getNext().setNext(head);// 将当前结点的指针域指向前一结点 | |
head.setNext(null);// 前一结点的指针域令为null; | |
return reHead;// 反转后新链表的头结点 | |
} | |
} | |
class Node { | |
private int Data;// 数据域 | |
private Node Next;// 指针域 | |
public Node(int Data) { | |
// super(); | |
this.Data = Data; | |
} | |
public int getData() { | |
return Data; | |
} | |
public void setData(int Data) { | |
this.Data = Data; | |
} | |
public Node getNext() { | |
return Next; | |
} | |
public void setNext(Node Next) { | |
this.Next = Next; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment