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

1. 找出两个链表的交点

  1. Intersection of Two Linked Lists (Easy)

Leetcode / 力扣

例如以下示例中 A 和 B 两个链表相交于 c1:

  1. A: a1 → a2
  2. c1 → c2 → c3
  3. B: b1 → b2 → b3

但是不会出现以下相交的情况,因为每个节点只有一个 next 指针,也就只能有一个后继节点,而以下示例中节点 c 有两个后继节点。

  1. A: a1 → a2 d1 → d2
  2. ↘ ↗
  3. c
  4. ↗ ↘
  5. B: b1 → b2 → b3 e1 → e2

要求时间复杂度为 O(N),空间复杂度为 O(1)。如果不存在交点则返回 null。

设 A 的长度为 a + c,B 的长度为 b + c,其中 c 为尾部公共部分长度,可知 a + c + b = b + c + a。

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

如果不存在交点,那么 a + b = b + a,以下实现代码中 l1 和 l2 会同时为 null,从而退出循环。

  1. public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
  2. ListNode l1 = headA, l2 = headB;
  3. while (l1 != l2) {
  4. l1 = (l1 == null) ? headB : l1.next;
  5. l2 = (l2 == null) ? headA : l2.next;
  6. }
  7. return l1;
  8. }

如果只是判断是否存在交点,那么就是另一个问题,即 编程之美 3.6 的问题。有两种解法:

  • 把第一个链表的结尾连接到第二个链表的开头,看第二个链表是否存在环;
  • 或者直接比较两个链表的最后一个节点是否相同。

2. 链表反转

  1. Reverse Linked List (Easy)

Leetcode / 力扣

递归

  1. public ListNode reverseList(ListNode head) {
  2. if (head == null || head.next == null) {
  3. return head;
  4. }
  5. ListNode next = head.next;
  6. ListNode newHead = reverseList(next);
  7. next.next = head;
  8. head.next = null;
  9. return newHead;
  10. }

头插法

  1. public ListNode reverseList(ListNode head) {
  2. ListNode newHead = new ListNode(-1);
  3. while (head != null) {
  4. ListNode next = head.next;
  5. head.next = newHead.next;
  6. newHead.next = head;
  7. head = next;
  8. }
  9. return newHead.next;
  10. }

3. 归并两个有序的链表

  1. Merge Two Sorted Lists (Easy)

Leetcode / 力扣

  1. public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
  2. if (l1 == null) return l2;
  3. if (l2 == null) return l1;
  4. if (l1.val < l2.val) {
  5. l1.next = mergeTwoLists(l1.next, l2);
  6. return l1;
  7. } else {
  8. l2.next = mergeTwoLists(l1, l2.next);
  9. return l2;
  10. }
  11. }

4. 从有序链表中删除重复节点

  1. Remove Duplicates from Sorted List (Easy)

Leetcode / 力扣

  1. Given 1->1->2, return 1->2.
  2. Given 1->1->2->3->3, return 1->2->3.
  1. public ListNode deleteDuplicates(ListNode head) {
  2. if (head == null || head.next == null) return head;
  3. head.next = deleteDuplicates(head.next);
  4. return head.val == head.next.val ? head.next : head;
  5. }

5. 删除链表的倒数第 n 个节点

  1. Remove Nth Node From End of List (Medium)

Leetcode / 力扣

  1. Given linked list: 1->2->3->4->5, and n = 2.
  2. After removing the second node from the end, the linked list becomes 1->2->3->5.
  1. public ListNode removeNthFromEnd(ListNode head, int n) {
  2. ListNode fast = head;
  3. while (n-- > 0) {
  4. fast = fast.next;
  5. }
  6. if (fast == null) return head.next;
  7. ListNode slow = head;
  8. while (fast.next != null) {
  9. fast = fast.next;
  10. slow = slow.next;
  11. }
  12. slow.next = slow.next.next;
  13. return head;
  14. }

6. 交换链表中的相邻结点

  1. Swap Nodes in Pairs (Medium)

Leetcode / 力扣

  1. Given 1->2->3->4, you should return the list as 2->1->4->3.

题目要求:不能修改结点的 val 值,O(1) 空间复杂度。

  1. public ListNode swapPairs(ListNode head) {
  2. ListNode node = new ListNode(-1);
  3. node.next = head;
  4. ListNode pre = node;
  5. while (pre.next != null && pre.next.next != null) {
  6. ListNode l1 = pre.next, l2 = pre.next.next;
  7. ListNode next = l2.next;
  8. l1.next = next;
  9. l2.next = l1;
  10. pre.next = l2;
  11. pre = l1;
  12. }
  13. return node.next;
  14. }

7. 链表求和

  1. Add Two Numbers II (Medium)

Leetcode / 力扣

  1. Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
  2. Output: 7 -> 8 -> 0 -> 7

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

  1. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  2. Stack<Integer> l1Stack = buildStack(l1);
  3. Stack<Integer> l2Stack = buildStack(l2);
  4. ListNode head = new ListNode(-1);
  5. int carry = 0;
  6. while (!l1Stack.isEmpty() || !l2Stack.isEmpty() || carry != 0) {
  7. int x = l1Stack.isEmpty() ? 0 : l1Stack.pop();
  8. int y = l2Stack.isEmpty() ? 0 : l2Stack.pop();
  9. int sum = x + y + carry;
  10. ListNode node = new ListNode(sum % 10);
  11. node.next = head.next;
  12. head.next = node;
  13. carry = sum / 10;
  14. }
  15. return head.next;
  16. }
  17. private Stack<Integer> buildStack(ListNode l) {
  18. Stack<Integer> stack = new Stack<>();
  19. while (l != null) {
  20. stack.push(l.val);
  21. l = l.next;
  22. }
  23. return stack;
  24. }

8. 回文链表

  1. Palindrome Linked List (Easy)

Leetcode / 力扣

题目要求:以 O(1) 的空间复杂度来求解。

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

  1. public boolean isPalindrome(ListNode head) {
  2. if (head == null || head.next == null) return true;
  3. ListNode slow = head, fast = head.next;
  4. while (fast != null && fast.next != null) {
  5. slow = slow.next;
  6. fast = fast.next.next;
  7. }
  8. if (fast != null) slow = slow.next; // 偶数节点,让 slow 指向下一个节点
  9. cut(head, slow); // 切成两个链表
  10. return isEqual(head, reverse(slow));
  11. }
  12. private void cut(ListNode head, ListNode cutNode) {
  13. while (head.next != cutNode) {
  14. head = head.next;
  15. }
  16. head.next = null;
  17. }
  18. private ListNode reverse(ListNode head) {
  19. ListNode newHead = null;
  20. while (head != null) {
  21. ListNode nextNode = head.next;
  22. head.next = newHead;
  23. newHead = head;
  24. head = nextNode;
  25. }
  26. return newHead;
  27. }
  28. private boolean isEqual(ListNode l1, ListNode l2) {
  29. while (l1 != null && l2 != null) {
  30. if (l1.val != l2.val) return false;
  31. l1 = l1.next;
  32. l2 = l2.next;
  33. }
  34. return true;
  35. }

9. 分隔链表

  1. Split Linked List in Parts(Medium)

Leetcode / 力扣

  1. Input:
  2. root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
  3. Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
  4. Explanation:
  5. 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.

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

  1. public ListNode[] splitListToParts(ListNode root, int k) {
  2. int N = 0;
  3. ListNode cur = root;
  4. while (cur != null) {
  5. N++;
  6. cur = cur.next;
  7. }
  8. int mod = N % k;
  9. int size = N / k;
  10. ListNode[] ret = new ListNode[k];
  11. cur = root;
  12. for (int i = 0; cur != null && i < k; i++) {
  13. ret[i] = cur;
  14. int curSize = size + (mod-- > 0 ? 1 : 0);
  15. for (int j = 0; j < curSize - 1; j++) {
  16. cur = cur.next;
  17. }
  18. ListNode next = cur.next;
  19. cur.next = null;
  20. cur = next;
  21. }
  22. return ret;
  23. }

10. 链表元素按奇偶聚集

  1. Odd Even Linked List (Medium)

Leetcode / 力扣

  1. Example:
  2. Given 1->2->3->4->5->NULL,
  3. return 1->3->5->2->4->NULL.
  1. public ListNode oddEvenList(ListNode head) {
  2. if (head == null) {
  3. return head;
  4. }
  5. ListNode odd = head, even = head.next, evenHead = even;
  6. while (even != null && even.next != null) {
  7. odd.next = odd.next.next;
  8. odd = odd.next;
  9. even.next = even.next.next;
  10. even = even.next;
  11. }
  12. odd.next = evenHead;
  13. return head;
  14. }