Leetcode 题解 - 链表.md 9.0 KB
Newer Older
C
CyC2018 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!-- GFM-TOC -->
* [找出两个链表的交点](#找出两个链表的交点)
* [链表反转](#链表反转)
* [归并两个有序的链表](#归并两个有序的链表)
* [从有序链表中删除重复节点](#从有序链表中删除重复节点)
* [删除链表的倒数第 n 个节点](#删除链表的倒数第-n-个节点)
* [交换链表中的相邻结点](#交换链表中的相邻结点)
* [链表求和](#链表求和)
* [回文链表](#回文链表)
* [分隔链表](#分隔链表)
* [链表元素按奇偶聚集](#链表元素按奇偶聚集)
<!-- GFM-TOC -->


C
CyC2018 已提交
15 16
链表是空节点,或者有一个值和一个指向下一个链表的指针,因此很多链表问题可以用递归来处理。

C
CyC2018 已提交
17
#  找出两个链表的交点
C
CyC2018 已提交
18

C
CyC2018 已提交
19
[160. Intersection of Two Linked Lists (Easy)](https://leetcode.com/problems/intersection-of-two-linked-lists/description/)
C
CyC2018 已提交
20 21

```html
C
CyC2018 已提交
22 23 24 25 26
A:          a1 → a2

                      c1 → c2 → c3

B:    b1 → b2 → b3
C
CyC2018 已提交
27 28
```

C
CyC2018 已提交
29
要求:时间复杂度为 O(N),空间复杂度为 O(1)
C
CyC2018 已提交
30

C
CyC2018 已提交
31
设 A 的长度为 a + c,B 的长度为 b + c,其中 c 为尾部公共部分长度,可知 a + c + b = b + c + a。
C
CyC2018 已提交
32

C
CyC2018 已提交
33
当访问 A 链表的指针访问到链表尾部时,令它从链表 B 的头部开始访问链表 B;同样地,当访问 B 链表的指针访问到链表尾部时,令它从链表 A 的头部开始访问链表 A。这样就能控制访问 A 和 B 两个链表的指针能同时访问到交点。
C
CyC2018 已提交
34 35

```java
C
CyC2018 已提交
36 37 38 39 40 41 42
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    ListNode l1 = headA, l2 = headB;
    while (l1 != l2) {
        l1 = (l1 == null) ? headB : l1.next;
        l2 = (l2 == null) ? headA : l2.next;
    }
    return l1;
C
CyC2018 已提交
43 44 45
}
```

C
CyC2018 已提交
46
如果只是判断是否存在交点,那么就是另一个问题,即 [编程之美 3.6]() 的问题。有两种解法:
C
CyC2018 已提交
47

C
CyC2018 已提交
48 49
- 把第一个链表的结尾连接到第二个链表的开头,看第二个链表是否存在环;
- 或者直接比较两个链表的最后一个节点是否相同。
C
CyC2018 已提交
50

C
CyC2018 已提交
51
#  链表反转
C
CyC2018 已提交
52

C
CyC2018 已提交
53
[206. Reverse Linked List (Easy)](https://leetcode.com/problems/reverse-linked-list/description/)
C
CyC2018 已提交
54 55 56 57

递归

```java
C
CyC2018 已提交
58 59 60 61 62 63 64 65 66
public ListNode reverseList(ListNode head) {
    if (head == null || head.next == null) {
        return head;
    }
    ListNode next = head.next;
    ListNode newHead = reverseList(next);
    next.next = head;
    head.next = null;
    return newHead;
C
CyC2018 已提交
67 68 69 70 71 72
}
```

头插法

```java
C
CyC2018 已提交
73 74 75 76 77 78 79 80 81
public ListNode reverseList(ListNode head) {
    ListNode newHead = new ListNode(-1);
    while (head != null) {
        ListNode next = head.next;
        head.next = newHead.next;
        newHead.next = head;
        head = next;
    }
    return newHead.next;
C
CyC2018 已提交
82 83 84
}
```

C
CyC2018 已提交
85
#  归并两个有序的链表
C
CyC2018 已提交
86

C
CyC2018 已提交
87
[21. Merge Two Sorted Lists (Easy)](https://leetcode.com/problems/merge-two-sorted-lists/description/)
C
CyC2018 已提交
88 89

```java
C
CyC2018 已提交
90 91 92 93 94 95 96 97 98 99
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    if (l1 == null) return l2;
    if (l2 == null) return l1;
    if (l1.val < l2.val) {
        l1.next = mergeTwoLists(l1.next, l2);
        return l1;
    } else {
        l2.next = mergeTwoLists(l1, l2.next);
        return l2;
    }
C
CyC2018 已提交
100 101 102
}
```

C
CyC2018 已提交
103
#  从有序链表中删除重复节点
C
CyC2018 已提交
104

C
CyC2018 已提交
105
[83. Remove Duplicates from Sorted List (Easy)](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/)
C
CyC2018 已提交
106 107

```html
C
CyC2018 已提交
108 109
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
C
CyC2018 已提交
110 111 112
```

```java
C
CyC2018 已提交
113 114 115 116
public ListNode deleteDuplicates(ListNode head) {
    if (head == null || head.next == null) return head;
    head.next = deleteDuplicates(head.next);
    return head.val == head.next.val ? head.next : head;
C
CyC2018 已提交
117 118 119
}
```

C
CyC2018 已提交
120
#  删除链表的倒数第 n 个节点
C
CyC2018 已提交
121

C
CyC2018 已提交
122
[19. Remove Nth Node From End of List (Medium)](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/)
C
CyC2018 已提交
123 124

```html
C
CyC2018 已提交
125 126
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
C
CyC2018 已提交
127 128 129
```

```java
C
CyC2018 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142
public ListNode removeNthFromEnd(ListNode head, int n) {
    ListNode fast = head;
    while (n-- > 0) {
        fast = fast.next;
    }
    if (fast == null) return head.next;
    ListNode slow = head;
    while (fast.next != null) {
        fast = fast.next;
        slow = slow.next;
    }
    slow.next = slow.next.next;
    return head;
C
CyC2018 已提交
143 144 145
}
```

C
CyC2018 已提交
146
#  交换链表中的相邻结点
C
CyC2018 已提交
147

C
CyC2018 已提交
148
[24. Swap Nodes in Pairs (Medium)](https://leetcode.com/problems/swap-nodes-in-pairs/description/)
C
CyC2018 已提交
149 150

```html
C
CyC2018 已提交
151
Given 1->2->3->4, you should return the list as 2->1->4->3.
C
CyC2018 已提交
152 153
```

C
CyC2018 已提交
154
题目要求:不能修改结点的 val 值,O(1) 空间复杂度。
C
CyC2018 已提交
155 156

```java
C
CyC2018 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170
public ListNode swapPairs(ListNode head) {
    ListNode node = new ListNode(-1);
    node.next = head;
    ListNode pre = node;
    while (pre.next != null && pre.next.next != null) {
        ListNode l1 = pre.next, l2 = pre.next.next;
        ListNode next = l2.next;
        l1.next = next;
        l2.next = l1;
        pre.next = l2;

        pre = l1;
    }
    return node.next;
C
CyC2018 已提交
171 172 173
}
```

C
CyC2018 已提交
174
#  链表求和
C
CyC2018 已提交
175

C
CyC2018 已提交
176
[445. Add Two Numbers II (Medium)](https://leetcode.com/problems/add-two-numbers-ii/description/)
C
CyC2018 已提交
177 178

```html
C
CyC2018 已提交
179 180
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
C
CyC2018 已提交
181 182 183 184 185
```

题目要求:不能修改原始链表。

```java
C
CyC2018 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    Stack<Integer> l1Stack = buildStack(l1);
    Stack<Integer> l2Stack = buildStack(l2);
    ListNode head = new ListNode(-1);
    int carry = 0;
    while (!l1Stack.isEmpty() || !l2Stack.isEmpty() || carry != 0) {
        int x = l1Stack.isEmpty() ? 0 : l1Stack.pop();
        int y = l2Stack.isEmpty() ? 0 : l2Stack.pop();
        int sum = x + y + carry;
        ListNode node = new ListNode(sum % 10);
        node.next = head.next;
        head.next = node;
        carry = sum / 10;
    }
    return head.next;
C
CyC2018 已提交
201 202
}

C
CyC2018 已提交
203 204 205 206 207 208 209
private Stack<Integer> buildStack(ListNode l) {
    Stack<Integer> stack = new Stack<>();
    while (l != null) {
        stack.push(l.val);
        l = l.next;
    }
    return stack;
C
CyC2018 已提交
210 211 212
}
```

C
CyC2018 已提交
213
#  回文链表
C
CyC2018 已提交
214

C
CyC2018 已提交
215
[234. Palindrome Linked List (Easy)](https://leetcode.com/problems/palindrome-linked-list/description/)
C
CyC2018 已提交
216

C
CyC2018 已提交
217
题目要求:以 O(1) 的空间复杂度来求解。
C
CyC2018 已提交
218 219 220 221

切成两半,把后半段反转,然后比较两半是否相等。

```java
C
CyC2018 已提交
222 223 224 225 226 227 228 229 230 231
public boolean isPalindrome(ListNode head) {
    if (head == null || head.next == null) return true;
    ListNode slow = head, fast = head.next;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    if (fast != null) slow = slow.next;  // 偶数节点,让 slow 指向下一个节点
    cut(head, slow);                     // 切成两个链表
    return isEqual(head, reverse(slow));
C
CyC2018 已提交
232 233
}

C
CyC2018 已提交
234 235 236 237 238
private void cut(ListNode head, ListNode cutNode) {
    while (head.next != cutNode) {
        head = head.next;
    }
    head.next = null;
C
CyC2018 已提交
239 240
}

C
CyC2018 已提交
241 242 243 244 245 246 247 248 249
private ListNode reverse(ListNode head) {
    ListNode newHead = null;
    while (head != null) {
        ListNode nextNode = head.next;
        head.next = newHead;
        newHead = head;
        head = nextNode;
    }
    return newHead;
C
CyC2018 已提交
250 251
}

C
CyC2018 已提交
252 253 254 255 256 257 258
private boolean isEqual(ListNode l1, ListNode l2) {
    while (l1 != null && l2 != null) {
        if (l1.val != l2.val) return false;
        l1 = l1.next;
        l2 = l2.next;
    }
    return true;
C
CyC2018 已提交
259 260 261
}
```

C
CyC2018 已提交
262
#  分隔链表
C
CyC2018 已提交
263

C
CyC2018 已提交
264
[725. Split Linked List in Parts(Medium)](https://leetcode.com/problems/split-linked-list-in-parts/description/)
C
CyC2018 已提交
265 266 267

```html
Input:
C
CyC2018 已提交
268 269
root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
C
CyC2018 已提交
270
Explanation:
C
CyC2018 已提交
271
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
C
CyC2018 已提交
272 273
```

C
CyC2018 已提交
274
题目描述:把链表分隔成 k 部分,每部分的长度都应该尽可能相同,排在前面的长度应该大于等于后面的。
C
CyC2018 已提交
275 276

```java
C
CyC2018 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
public ListNode[] splitListToParts(ListNode root, int k) {
    int N = 0;
    ListNode cur = root;
    while (cur != null) {
        N++;
        cur = cur.next;
    }
    int mod = N % k;
    int size = N / k;
    ListNode[] ret = new ListNode[k];
    cur = root;
    for (int i = 0; cur != null && i < k; i++) {
        ret[i] = cur;
        int curSize = size + (mod-- > 0 ? 1 : 0);
        for (int j = 0; j < curSize - 1; j++) {
            cur = cur.next;
        }
        ListNode next = cur.next;
        cur.next = null;
        cur = next;
    }
    return ret;
C
CyC2018 已提交
299 300 301
}
```

C
CyC2018 已提交
302
#  链表元素按奇偶聚集
C
CyC2018 已提交
303

C
CyC2018 已提交
304
[328. Odd Even Linked List (Medium)](https://leetcode.com/problems/odd-even-linked-list/description/)
C
CyC2018 已提交
305 306 307

```html
Example:
C
CyC2018 已提交
308 309
Given 1->2->3->4->5->NULL,
return 1->3->5->2->4->NULL.
C
CyC2018 已提交
310 311 312
```

```java
C
CyC2018 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325
public ListNode oddEvenList(ListNode head) {
    if (head == null) {
        return head;
    }
    ListNode odd = head, even = head.next, evenHead = even;
    while (even != null && even.next != null) {
        odd.next = odd.next.next;
        odd = odd.next;
        even.next = even.next.next;
        even = even.next;
    }
    odd.next = evenHead;
    return head;
C
CyC2018 已提交
326 327
}
```
C
CyC2018 已提交
328 329 330 331 332 333 334




<div align="center">欢迎关注公众号,获取最新文章!</div>


C
CyC2018 已提交
335
<div align="center"><img width="180px" src="https://cyc-1256109796.cos.ap-guangzhou.myqcloud.com/%E5%85%AC%E4%BC%97%E5%8F%B7.jpg"></img></div>