Skip to content

Commit 79cdb3a

Browse files
author
luzhipeng
committed
reverse linked list
1 parent 024f47c commit 79cdb3a

File tree

3 files changed

+227
-0
lines changed

3 files changed

+227
-0
lines changed

Diff for: 206.reverse-linked-list.md

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
## 题目地址
2+
https://leetcode.com/problems/reverse-linked-list/description/
3+
4+
## 题目描述
5+
Reverse a singly linked list.
6+
7+
Example:
8+
9+
Input: 1->2->3->4->5->NULL
10+
Output: 5->4->3->2->1->NULL
11+
Follow up:
12+
13+
A linked list can be reversed either iteratively or recursively. Could you implement both?
14+
15+
## 思路
16+
这个就是常规操作了,使用一个变量记录前驱pre,一个变量记录后继next.
17+
18+
不断更新`current.next = pre` 就好了
19+
## 关键点解析
20+
21+
- 链表的基本操作(交换)
22+
- 虚拟节点dummy 简化操作
23+
- 注意更新current和pre的位置, 否则有可能出现溢出
24+
25+
26+
## 代码
27+
28+
```js
29+
/*
30+
* @lc app=leetcode id=206 lang=javascript
31+
*
32+
* [206] Reverse Linked List
33+
*
34+
* https://leetcode.com/problems/reverse-linked-list/description/
35+
*
36+
* algorithms
37+
* Easy (52.95%)
38+
* Total Accepted: 532.6K
39+
* Total Submissions: 1M
40+
* Testcase Example: '[1,2,3,4,5]'
41+
*
42+
* Reverse a singly linked list.
43+
*
44+
* Example:
45+
*
46+
*
47+
* Input: 1->2->3->4->5->NULL
48+
* Output: 5->4->3->2->1->NULL
49+
*
50+
*
51+
* Follow up:
52+
*
53+
* A linked list can be reversed either iteratively or recursively. Could you
54+
* implement both?
55+
*
56+
*/
57+
/**
58+
* Definition for singly-linked list.
59+
* function ListNode(val) {
60+
* this.val = val;
61+
* this.next = null;
62+
* }
63+
*/
64+
/**
65+
* @param {ListNode} head
66+
* @return {ListNode}
67+
*/
68+
var reverseList = function(head) {
69+
const dummyHead = {
70+
next: head
71+
}
72+
let current = dummyHead.next;
73+
let pre = null;
74+
75+
while(current) {
76+
const next = current.next;
77+
current.next = pre;
78+
pre = current;
79+
current = next;
80+
}
81+
return pre;
82+
};
83+
84+
```
85+

Diff for: 92.reverse-linked-list-ii.md

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
## 题目地址
2+
https://leetcode.com/problems/reverse-linked-list-ii/description/
3+
4+
## 题目描述
5+
Reverse a linked list from position m to n. Do it in one-pass.
6+
7+
Note: 1 ≤ m ≤ n ≤ length of list.
8+
9+
Example:
10+
11+
Input: 1->2->3->4->5->NULL, m = 2, n = 4
12+
Output: 1->4->3->2->5->NULL
13+
14+
## 思路
15+
16+
考虑取出需要反转的这一小段链表,反转完后再插入到原先的链表中。
17+
18+
以本题为例:
19+
20+
变换的是2,3,4这三个点,那么我们可以先取出2,用front指针指向2,然后当取出3的时候,我们把3加到2的前面,把front指针前移到3,依次类推,到4后停止,这样我们得到一个新链表4->3->2, front指针指向4。
21+
22+
对于原链表来说,有两个点的位置很重要,需要用指针记录下来,分别是1和5,把新链表插入的时候需要这两个点的位置。
23+
24+
用pre指针记录1的位置
25+
26+
当4结点被取走后,5的位置需要记下来
27+
28+
这样我们就可以把倒置后的那一小段链表加入到原链表中
29+
30+
![92.reverse-linked-list-ii](./assets/92.reverse-linked-list-ii.gif)
31+
32+
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
33+
## 关键点解析
34+
35+
- 链表的基本操作(交换)
36+
- 虚拟节点dummy 简化操作
37+
- 考虑特殊情况 m 是 1 或者 n是链表长度的情况
38+
- 用四个变量记录特殊节点, 然后操作这四个节点使之按照一定方式连接即可。
39+
40+
```js
41+
let midStartNode = null;
42+
let preMidStartNode = null;
43+
let midEndNode = null;
44+
let postMidEndNode = null;
45+
```
46+
47+
- 注意更新current和pre的位置, 否则有可能出现溢出
48+
49+
50+
## 代码
51+
52+
```js
53+
/*
54+
* @lc app=leetcode id=92 lang=javascript
55+
*
56+
* [92] Reverse Linked List II
57+
*
58+
* https://leetcode.com/problems/reverse-linked-list-ii/description/
59+
*
60+
* algorithms
61+
* Medium (34.13%)
62+
* Total Accepted: 182.3K
63+
* Total Submissions: 532.8K
64+
* Testcase Example: '[1,2,3,4,5]\n2\n4'
65+
*
66+
* Reverse a linked list from position m to n. Do it in one-pass.
67+
*
68+
* Note: 1 ≤ m ≤ n ≤ length of list.
69+
*
70+
* Example:
71+
*
72+
*
73+
* Input: 1->2->3->4->5->NULL, m = 2, n = 4
74+
* Output: 1->4->3->2->5->NULL
75+
*
76+
*
77+
*/
78+
/**
79+
* Definition for singly-linked list.
80+
* function ListNode(val) {
81+
* this.val = val;
82+
* this.next = null;
83+
* }
84+
*/
85+
/**
86+
* @param {ListNode} head
87+
* @param {number} m
88+
* @param {number} n
89+
* @return {ListNode}
90+
*/
91+
var reverseBetween = function(head, m, n) {
92+
const dummyHead = {
93+
next: head
94+
}
95+
96+
let current = dummyHead.next;
97+
let pre = current;
98+
let index = 0;
99+
100+
let midStartNode = null;
101+
let preMidStartNode = null;
102+
let midEndNode = null;
103+
let postMidEndNode = null;
104+
105+
while(current) {
106+
const next = current.next;
107+
index++;
108+
109+
// reverse linked list
110+
if (index > m && index <= n) {
111+
current.next = pre;
112+
}
113+
114+
if (index === m - 1) {
115+
preMidStartNode = current;
116+
}
117+
if (index === m) {
118+
midStartNode = current;
119+
}
120+
121+
if (index === n + 1) {
122+
postMidEndNode = current;
123+
postMidEndNode.next
124+
}
125+
126+
if (index === n) {
127+
midEndNode = current;;
128+
}
129+
130+
pre = current;
131+
132+
current = next;
133+
}
134+
135+
// 两个链表合并起来
136+
(preMidStartNode || dummyHead).next = midEndNode; // 特殊情况需要考虑
137+
midStartNode.next = postMidEndNode;
138+
139+
return dummyHead.next;
140+
};
141+
142+
```

Diff for: assets/92.reverse-linked-list-ii.gif

489 KB
Loading

0 commit comments

Comments
 (0)