MCPcopy Create free account
hub / github.com/crossoverJie/JCSprout / ReverseNode

Class ReverseNode

src/main/java/com/crossoverjie/algorithm/ReverseNode.java:12–106  ·  view source on GitHub ↗

Function: 三种方式反向打印单向链表 @author crossoverJie Date: 10/02/2018 16:14 @since JDK 1.8

Source from the content-addressed store, hash-verified

10 * @since JDK 1.8
11 */
12public class ReverseNode {
13
14
15 /**
16 * 利用栈的先进后出特性
17 * @param node
18 */
19 public void reverseNode1(Node node){
20
21 System.out.println("====翻转之前====");
22
23 Stack<Node> stack = new Stack<>() ;
24 while (node != null){
25
26 System.out.print(node.value + "===>");
27
28 stack.push(node) ;
29 node = node.next ;
30 }
31
32 System.out.println("");
33
34 System.out.println("====翻转之后====");
35 while (!stack.isEmpty()){
36 System.out.print(stack.pop().value + "===>");
37 }
38
39 }
40
41
42 /**
43 * 利用头插法插入链表
44 * @param head
45 */
46 public void reverseNode(Node head) {
47 if (head == null) {
48 return ;
49 }
50
51 //最终翻转之后的 Node
52 Node node ;
53
54 Node pre = head;
55 Node cur = head.next;
56 Node next ;
57 while(cur != null){
58 next = cur.next;
59
60 //链表的头插法
61 cur.next = pre;
62 pre = cur;
63
64 cur = next;
65 }
66 head.next = null;
67 node = pre;
68
69

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected