2019-04-21 10:36:08 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
2019-05-14 22:56:30 +08:00
|
|
|
|
* [1. 找出两个链表的交点](#1-找出两个链表的交点)
|
|
|
|
|
* [2. 链表反转](#2-链表反转)
|
|
|
|
|
* [3. 归并两个有序的链表](#3-归并两个有序的链表)
|
|
|
|
|
* [4. 从有序链表中删除重复节点](#4-从有序链表中删除重复节点)
|
|
|
|
|
* [5. 删除链表的倒数第 n 个节点](#5-删除链表的倒数第-n-个节点)
|
|
|
|
|
* [6. 交换链表中的相邻结点](#6-交换链表中的相邻结点)
|
|
|
|
|
* [7. 链表求和](#7-链表求和)
|
|
|
|
|
* [8. 回文链表](#8-回文链表)
|
|
|
|
|
* [9. 分隔链表](#9-分隔链表)
|
|
|
|
|
* [10. 链表元素按奇偶聚集](#10-链表元素按奇偶聚集)
|
2019-04-21 10:36:08 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
2019-03-27 20:57:37 +08:00
|
|
|
|
|
|
|
|
|
|
2019-03-08 20:31:07 +08:00
|
|
|
|
链表是空节点,或者有一个值和一个指向下一个链表的指针,因此很多链表问题可以用递归来处理。
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
# 1. 找出两个链表的交点
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[160. Intersection of Two Linked Lists (Easy)](https://leetcode.com/problems/intersection-of-two-linked-lists/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-06-04 21:41:17 +08:00
|
|
|
|
例如以下示例中 A 和 B 两个链表相交于 c1:
|
|
|
|
|
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```html
|
2019-03-27 20:57:37 +08:00
|
|
|
|
A: a1 → a2
|
|
|
|
|
↘
|
|
|
|
|
c1 → c2 → c3
|
|
|
|
|
↗
|
|
|
|
|
B: b1 → b2 → b3
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-06-04 21:41:17 +08:00
|
|
|
|
但是不会出现以下相交的情况,因为每个节点只有一个 next 指针,也就只能有一个后继节点,而以下示例中节点 c 有两个后继节点。
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
A: a1 → a2 d1 → d2
|
|
|
|
|
↘ ↗
|
|
|
|
|
c
|
|
|
|
|
↗ ↘
|
|
|
|
|
B: b1 → b2 → b3 e1 → e2
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
要求时间复杂度为 O(N),空间复杂度为 O(1)。如果不存在交点则返回 null。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
设 A 的长度为 a + c,B 的长度为 b + c,其中 c 为尾部公共部分长度,可知 a + c + b = b + c + a。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
当访问 A 链表的指针访问到链表尾部时,令它从链表 B 的头部开始访问链表 B;同样地,当访问 B 链表的指针访问到链表尾部时,令它从链表 A 的头部开始访问链表 A。这样就能控制访问 A 和 B 两个链表的指针能同时访问到交点。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-06-04 21:41:17 +08:00
|
|
|
|
如果不存在交点,那么 a + b = b + a,以下实现代码中 l1 和 l2 会同时为 null,从而退出循环。
|
|
|
|
|
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
如果只是判断是否存在交点,那么就是另一个问题,即 [编程之美 3.6]() 的问题。有两种解法:
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
- 把第一个链表的结尾连接到第二个链表的开头,看第二个链表是否存在环;
|
|
|
|
|
- 或者直接比较两个链表的最后一个节点是否相同。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
# 2. 链表反转
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[206. Reverse Linked List (Easy)](https://leetcode.com/problems/reverse-linked-list/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
递归
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
头插法
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
# 3. 归并两个有序的链表
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[21. Merge Two Sorted Lists (Easy)](https://leetcode.com/problems/merge-two-sorted-lists/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
# 4. 从有序链表中删除重复节点
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[83. Remove Duplicates from Sorted List (Easy)](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Given 1->1->2, return 1->2.
|
|
|
|
|
Given 1->1->2->3->3, return 1->2->3.
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
# 5. 删除链表的倒数第 n 个节点
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[19. Remove Nth Node From End of List (Medium)](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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.
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
# 6. 交换链表中的相邻结点
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[24. Swap Nodes in Pairs (Medium)](https://leetcode.com/problems/swap-nodes-in-pairs/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Given 1->2->3->4, you should return the list as 2->1->4->3.
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
题目要求:不能修改结点的 val 值,O(1) 空间复杂度。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
# 7. 链表求和
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[445. Add Two Numbers II (Medium)](https://leetcode.com/problems/add-two-numbers-ii/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
|
|
|
|
|
Output: 7 -> 8 -> 0 -> 7
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
题目要求:不能修改原始链表。
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
private Stack<Integer> buildStack(ListNode l) {
|
|
|
|
|
Stack<Integer> stack = new Stack<>();
|
|
|
|
|
while (l != null) {
|
|
|
|
|
stack.push(l.val);
|
|
|
|
|
l = l.next;
|
|
|
|
|
}
|
|
|
|
|
return stack;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
# 8. 回文链表
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[234. Palindrome Linked List (Easy)](https://leetcode.com/problems/palindrome-linked-list/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
题目要求:以 O(1) 的空间复杂度来求解。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
切成两半,把后半段反转,然后比较两半是否相等。
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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));
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
private void cut(ListNode head, ListNode cutNode) {
|
|
|
|
|
while (head.next != cutNode) {
|
|
|
|
|
head = head.next;
|
|
|
|
|
}
|
|
|
|
|
head.next = null;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
private ListNode reverse(ListNode head) {
|
|
|
|
|
ListNode newHead = null;
|
|
|
|
|
while (head != null) {
|
|
|
|
|
ListNode nextNode = head.next;
|
|
|
|
|
head.next = newHead;
|
|
|
|
|
newHead = head;
|
|
|
|
|
head = nextNode;
|
|
|
|
|
}
|
|
|
|
|
return newHead;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
# 9. 分隔链表
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[725. Split Linked List in Parts(Medium)](https://leetcode.com/problems/split-linked-list-in-parts/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Input:
|
2019-03-27 20:57:37 +08:00
|
|
|
|
root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
|
|
|
|
|
Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
|
2019-03-08 20:31:07 +08:00
|
|
|
|
Explanation:
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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.
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
题目描述:把链表分隔成 k 部分,每部分的长度都应该尽可能相同,排在前面的长度应该大于等于后面的。
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-05-14 22:56:30 +08:00
|
|
|
|
# 10. 链表元素按奇偶聚集
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
2019-03-27 20:57:37 +08:00
|
|
|
|
[328. Odd Even Linked List (Medium)](https://leetcode.com/problems/odd-even-linked-list/description/)
|
2019-03-08 20:31:07 +08:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Example:
|
2019-03-27 20:57:37 +08:00
|
|
|
|
Given 1->2->3->4->5->NULL,
|
|
|
|
|
return 1->3->5->2->4->NULL.
|
2019-03-08 20:31:07 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-27 20:57:37 +08:00
|
|
|
|
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;
|
2019-03-08 20:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2019-03-27 20:57:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-06-13 13:31:54 +08:00
|
|
|
|
# 微信公众号
|
2019-06-10 11:23:18 +08:00
|
|
|
|
|
|
|
|
|
|
2019-06-18 00:57:23 +08:00
|
|
|
|
更多精彩内容将发布在微信公众号 CyC2018 上,你也可以在公众号后台和我交流学习和求职相关的问题。另外,公众号提供了该项目的 PDF 等离线阅读版本,后台回复 "下载" 即可领取。公众号也提供了一份技术面试复习大纲,不仅系统整理了面试知识点,而且标注了各个知识点的重要程度,从而帮你理清多而杂的面试知识点,后台回复 "大纲" 即可领取。我基本是按照这个大纲来进行复习的,对我拿到了 BAT 头条等 Offer 起到很大的帮助。你们完全可以和我一样根据大纲上列的知识点来进行复习,就不用看很多不重要的内容,也可以知道哪些内容很重要从而多安排一些复习时间。
|
2019-06-10 11:23:18 +08:00
|
|
|
|
|
|
|
|
|
|
2019-07-13 23:30:43 +08:00
|
|
|
|
<br><div align="center"><img width="320px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/other/公众号海报.png?v=1"></img></div>
|