LeetCode 206. Reverse Linked List [Easy]

反转单链表和双链表。

题目来源:https://leetcode.com/problems/reverse-linked-list

题目难度:Easy

解答1[Java]:

核心思想

设计两个指针,一个 prev指向前一个节点 ,一个cur 指向当前节点。最初的时候,prev指向null,cur指向第一个节点。最后 cur 指向 null 的时候,prev 刚好指向最后一个节点。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}

}

解答2[Java]:

核心思想

主要是利用 head.next.next = head; 相当于把当前节点的下一个节点指向了当前节点。递归调用到最后,p 一直是指向最后一个节点,并且不断被返回到上一层调用的。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode p = reverseList(head.next);
head.next.next = head;
head.next = null;
return p;
}
}

拓展:反转双向链表

核心思路

代码

双向链表节点定义

1
2
3
4
5
6
7
8
9
public class DoubleNode {
public int value;
public DoubleNode last;
public DoubleNode next;

public DoubleNode(int data) {
this.value = data;
}
}

算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public static DoubleNode reverseList(DoubleNode head) {
DoubleNode pre = null;
DoubleNode post = null;
while (head != null) {
post = head.next;
head.next = pre;
head.last = post;
pre = head;
head = post;
}
return pre;
}
}

完整测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
public class Code_07_ReverseList {
public static class Node {
public int value;
public Node next;

public Node(int data) {
this.value = data;
}
}

public static Node reverseList(Node head) {
Node pre = null;
Node post = null;
while (head != null) {
post = head.next;
head.next = pre;
pre = head;
head = post;
}
return pre;
}

public static class DoubleNode {
public int value;
public DoubleNode last;
public DoubleNode next;

public DoubleNode(int data) {
this.value = data;
}
}

public static DoubleNode reverseList(DoubleNode head) {
DoubleNode pre = null;
DoubleNode post = null;
while (head != null) {
post = head.next;
head.next = pre;
head.last = post;
pre = head;
head = post;
}
return pre;
}

public static void printLinkedList(Node head) {
System.out.print("Linked List: ");
while (head != null) {
System.out.print(head.value + " ");
head = head.next;
}
System.out.println();
}

public static void printDoubleLinkedList(DoubleNode head) {
System.out.print("Double Linked List: ");
DoubleNode end = null;
while (head != null) {
System.out.print(head.value + " ");
end = head;
head = head.next;
}
System.out.print("| ");
while (end != null) {
System.out.print(end.value + " ");
end = end.last;
}
System.out.println();
}

public static void main(String[] args) {
Node head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
printLinkedList(head1);
head1 = reverseList(head1);
printLinkedList(head1);

DoubleNode head2 = new DoubleNode(1);
head2.next = new DoubleNode(2);
head2.next.last = head2;
head2.next.next = new DoubleNode(3);
head2.next.next.last = head2.next;
head2.next.next.next = new DoubleNode(4);
head2.next.next.next.last = head2.next.next;
printDoubleLinkedList(head2);
printDoubleLinkedList(reverseList(head2));

}

}