-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathReverseLinkedList.java
More file actions
87 lines (78 loc) · 2.16 KB
/
Copy pathReverseLinkedList.java
File metadata and controls
87 lines (78 loc) · 2.16 KB
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
/**
* 给你单链表的头节点 <code>head</code> ,请你反转链表,并返回反转后的链表。
* <div class="original__bRMd">
* <div>
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
* <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
* <pre>
* <strong>输入:</strong>head = [1,2,3,4,5]
* <strong>输出:</strong>[5,4,3,2,1]
* </pre>
*
* <p><strong>示例 2:</strong></p>
* <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" />
* <pre>
* <strong>输入:</strong>head = [1,2]
* <strong>输出:</strong>[2,1]
* </pre>
*
* <p><strong>示例 3:</strong></p>
*
* <pre>
* <strong>输入:</strong>head = []
* <strong>输出:</strong>[]
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li>链表中节点的数目范围是 <code>[0, 5000]</code></li>
* <li><code>-5000 <= Node.val <= 5000</code></li>
* </ul>
*
* <p> </p>
*
* <p><strong>进阶:</strong>链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题?</p>
* </div>
* </div>
* <div><div>Related Topics</div><div><li>递归</li><li>链表</li></div></div><br><div><li>👍 2191</li><li>👎 0</li></div>
*/
package leetcode1;
public class ReverseLinkedList {
public static void main(String[] args) {
Solution solution = new ReverseLinkedList().new Solution();
}
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
/**
* 懵逼的话,画个图,理解理解
* 常规三指针法
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
while (head != null) {
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
}
}