数组和链表

203.移除链表元素 (简单)

  1. class Solution {
  2. public ListNode removeElements(ListNode head, int val) {
  3. // 哨兵节点
  4. ListNode dumyNode = new ListNode(), tmp = dumyNode;
  5. while(head != null) {
  6. if(head.val != val) {
  7. tmp.next = head;
  8. tmp = tmp.next;
  9. }
  10. head = head.next;
  11. }
  12. // 断开节点引用
  13. tmp.next = null;
  14. return dumyNode.next;
  15. }
  16. }

876.链表的中间结点(简单)

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. public ListNode middleNode(ListNode head) {
  13. // 快慢指针
  14. ListNode slow = head, fast = head;
  15. while(fast != null && fast.next != null) {
  16. slow = slow.next;
  17. fast = fast.next.next;
  18. }
  19. return slow;
  20. }
  21. }

83.删除排序链表中的重复元素(简单)

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. public ListNode deleteDuplicates(ListNode head) {
  13. ListNode resNode = new ListNode(-101), tmp = resNode;
  14. while(head != null) {
  15. if(tmp.val != head.val) {
  16. tmp.next = head;
  17. tmp = tmp.next;
  18. }
  19. head = head.next;
  20. }
  21. // 注意点,考察点
  22. tmp.next = null;
  23. return resNode.next;
  24. }
  25. }

剑指Offer 25.合并两个排序的链表 (中等)

  1. class Solution {
  2. public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
  3. // 哨兵节点 + 遍历
  4. ListNode dumyHead = new ListNode(), tmp = dumyHead;
  5. while(l1 != null && l2 != null) {
  6. if(l1.val <= l2.val) {
  7. tmp.next = l1;
  8. l1 = l1.next;
  9. } else {
  10. tmp.next = l2;
  11. l2 = l2.next;
  12. }
  13. tmp = tmp.next;
  14. }
  15. if(l1 != null) {
  16. tmp.next = l1;
  17. }
  18. if(l2 != null) {
  19. tmp.next = l2;
  20. }
  21. return dumyHead.next;
  22. }
  23. }

2.两数相加 (中等)

206.反转链表 (中等)

递归

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. ListNode res = new ListNode();
  13. public ListNode reverseList(ListNode head) {
  14. // 递归
  15. if(head == null) {
  16. return null;
  17. }
  18. reverse(head).next = null;
  19. return res.next;
  20. }
  21. public ListNode reverse(ListNode node) {
  22. if(node.next == null) {
  23. res.next = node;
  24. return node;
  25. }
  26. ListNode tmp = reverse(node.next);
  27. tmp.next = node;
  28. return node;
  29. }
  30. }

哨兵节点 + 头插法

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. public ListNode reverseList(ListNode head) {
  13. // 虚拟头结点 + 头插法
  14. ListNode dumyNode = new ListNode();
  15. while(head != null) {
  16. ListNode next = dumyNode.next;
  17. ListNode tmp = head.next;
  18. dumyNode.next = head;
  19. head.next = next;
  20. head = tmp;
  21. }
  22. return dumyNode.next;
  23. }
  24. }

234.回文链表 (中等)

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. public boolean isPalindrome(ListNode head) {
  13. ListNode slow = head, fast = head;
  14. while(fast != null && fast.next != null) {
  15. slow = slow.next;
  16. fast = fast.next.next;
  17. }
  18. ListNode newNode = reverseList(slow);
  19. while(newNode != null) {
  20. if(newNode.val != head.val) {
  21. return false;
  22. }
  23. head = head.next;
  24. newNode = newNode.next;
  25. }
  26. return true;
  27. }
  28. // 头插法,反转链表
  29. private ListNode reverseList(ListNode node) {
  30. ListNode newHead = null;
  31. ListNode p = node;
  32. // 反转后半段链表
  33. while(p != null) {
  34. ListNode tmp = p.next;
  35. p.next = newHead;
  36. newHead = p;
  37. p = tmp;
  38. }
  39. return newHead;
  40. }
  41. }

328.奇偶链表(中等)

  1. class Solution {
  2. public ListNode oddEvenList(ListNode head) {
  3. // 创建两个链表,一个只有奇数,一个只有偶数链表,遍历给定单链表,分别插入创建的链表,最终合并链表
  4. ListNode odd = new ListNode(), even = new ListNode(), res = odd, evenHead = even;
  5. int size = 1;
  6. while(head != null) {
  7. if(size % 2 != 0) {
  8. odd.next = head;
  9. odd = odd.next;
  10. } else {
  11. even.next = head;
  12. even = even.next;
  13. }
  14. head = head.next;
  15. size++;
  16. }
  17. odd.next = evenHead.next;
  18. even.next = null;
  19. return res.next;
  20. }
  21. }

25. K个一组翻转链表(困难)

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. public ListNode reverseKGroup(ListNode head, int k) {
  13. ListNode res = new ListNode(), p = head, tail = res;
  14. while(p != null) {
  15. int i = 1;
  16. ListNode tmpHead = p;
  17. // 1. 遍历k个结点,找到k范围内的头尾
  18. while(i < k && p != null) {
  19. p = p.next;
  20. i++;
  21. }
  22. if(p == null) {
  23. tail.next = tmpHead;
  24. return res.next;
  25. }
  26. ListNode tmp = p.next;
  27. ListNode[] resKGroup = reverse(tmpHead, p);
  28. tail.next = resKGroup[0];
  29. tail = resKGroup[1];
  30. p = tmp;
  31. }
  32. return res.next;
  33. }
  34. private ListNode[] reverse(ListNode head, ListNode tail) {
  35. ListNode newHead = null, p = head;
  36. while(p != tail) {
  37. ListNode tmp = p.next;
  38. p.next = newHead;
  39. newHead = p;
  40. p = tmp;
  41. }
  42. tail.next = newHead;
  43. return new ListNode[] {tail, head};
  44. }
  45. }

剑指Offer 22.链表中倒数第k个节点 (简单)

遍历计算总数

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10. public ListNode getKthFromEnd(ListNode head, int k) {
  11. int size = 0;
  12. ListNode tmp = head;
  13. while(tmp != null) {
  14. tmp = tmp.next;
  15. ++size;
  16. }
  17. for(int i = 0; i < size - k; i++) {
  18. head = head.next;
  19. }
  20. return head;
  21. }
  22. }

快慢指针

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10. public ListNode getKthFromEnd(ListNode head, int k) {
  11. ListNode slow = head, fast = head;
  12. while(--k >= 0) {
  13. fast = fast.next;
  14. }
  15. while(fast != null) {
  16. slow = slow.next;
  17. fast = fast.next;
  18. }
  19. return slow;
  20. }
  21. }

19.删除链表的倒数第N个结点 (中等)

二次遍历

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode() {}
  7. * ListNode(int val) { this.val = val; }
  8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  9. * }
  10. */
  11. class Solution {
  12. public ListNode removeNthFromEnd(ListNode head, int n) {
  13. if(head == null) {
  14. return null;
  15. }
  16. int size = 0;
  17. ListNode tmp = head, res = head, pre = head;
  18. while(tmp != null) {
  19. tmp = tmp.next;
  20. size++;
  21. }
  22. for(int i = 0; i < size - n; i++) {
  23. pre = head;
  24. head = head.next;
  25. }
  26. if(res == head) {
  27. return head.next;
  28. }
  29. pre.next = head.next;
  30. return res;
  31. }
  32. }

一次遍历-solution1

解题思路:

  • 双指针
    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode() {}
    7. * ListNode(int val) { this.val = val; }
    8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    9. * }
    10. */
    11. class Solution {
    12. public ListNode removeNthFromEnd(ListNode head, int n) {
    13. if(head == null) {
    14. return null;
    15. }
    16. // 双指针
    17. ListNode first = head, second = head, res = head;
    18. while(n-- >= 0) {
    19. if(first == null) {
    20. return res.next;
    21. }
    22. first = first.next;
    23. }
    24. while(first != null) {
    25. first = first.next;
    26. second = second.next;
    27. }
    28. second.next = second.next.next;
    29. return res;
    30. }
    31. }

一次遍历-solution2

解题思路:

  • 双指针+哨兵节点简化编程
    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode() {}
    7. * ListNode(int val) { this.val = val; }
    8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    9. * }
    10. */
    11. class Solution {
    12. public ListNode removeNthFromEnd(ListNode head, int n) {
    13. ListNode dumyHead = new ListNode(-1, head);
    14. ListNode slow = dumyHead, fast = dumyHead;
    15. for(int i = 0; i < n; i++) {
    16. fast = fast.next;
    17. }
    18. while(fast != null && fast.next != null) {
    19. slow = slow.next;
    20. fast = fast.next;
    21. }
    22. slow.next = slow.next.next;
    23. return dumyHead.next;
    24. }
    25. }

160.相交链表(简单)

快慢指针

解题思路:
A和B两个链表长度可能不同,但是A+B和B+A的长度是相同的,所以遍历A+B和遍历B+A一定是同时结束。 如果A,B相交的话A和B有一段尾巴是相同的,所以两个遍历的指针一定会同时到达交点 如果A,B不

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) {
  7. * val = x;
  8. * next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
  14. // 错的人迟早会走散,而对的人迟早会相逢!
  15. // A和B两个链表长度可能不同,但是A+B和B+A的长度是相同的,所以遍历A+B和遍历B+A一定是同时结束。 如果A,B相交的话A和B有一段尾巴是相同的,所以两个遍历的指针一定会同时到达交点 如果A,B不相交的话两个指针就会同时到达A+B(B+A)的尾节点
  16. if(headA == null || headB == null) {
  17. return null;
  18. }
  19. // 双指针
  20. ListNode heada = headA, headb = headB;
  21. while(heada != headb) {
  22. if(heada != null) {
  23. heada = heada.next;
  24. } else {
  25. heada = headB;
  26. }
  27. if(headb != null) {
  28. headb = headb.next;
  29. } else {
  30. headb = headA;
  31. }
  32. }
  33. return heada;
  34. }
  35. }

141.环形链表(简单)

快慢指针

  1. public class Solution {
  2. public boolean hasCycle(ListNode head) {
  3. // 链表判断是否存在环:快慢指针
  4. ListNode slow = head, fast = head;
  5. while(fast != null && fast.next != null) {
  6. slow = slow.next;
  7. fast = fast.next.next;
  8. if(slow == fast) {
  9. return true;
  10. }
  11. }
  12. return false;
  13. }
  14. }

栈和队列

剑指Offer 09.用两个栈实现队列(简单)

225.用队列实现栈(简单)

面试题03.05.栈排序(中等)

155.最小栈(简单)

面试题03.01.三合一(简单)

20.有效的括号(简单)

面试题16.26.计算器(中等)

772.基本计算器III(困难 包含括号 力扣会员)

1047.删除字符串中的所有相邻重复项(简单)

剑指Offer 31.栈的压入、弹出序列(中等)

  1. class Solution {
  2. public boolean validateStackSequences(int[] pushed, int[] popped) {
  3. if(pushed.length != popped.length) {
  4. return false;
  5. }
  6. // 用数组 stack 模拟栈
  7. int[] stack = new int[pushed.length];
  8. int popIdx = 0, cur = 0;
  9. for(int i = 0; i < pushed.length; i++) {
  10. // stack 模拟入栈
  11. stack[cur] = pushed[i];
  12. // 当 stack 入栈的元素等于 出栈序列 poped 的当前元素时,对应出栈
  13. while(cur >= 0 && stack[cur] == popped[popIdx]) {
  14. // 模拟出栈操作
  15. cur--;
  16. popIdx++;
  17. }
  18. cur++;
  19. }
  20. // popped 序列对应的是栈的弹出顺序,那么模拟栈 stack 的下标应该等于 0
  21. return cur == 0;
  22. }
  23. }

739.每日温度(中等) 单调栈

42.接雨水(困难)单调栈

84.柱状图中最大的矩形(困难)单调栈

面试题03.06.动物收容所(中等) 队列

剑指Offer 59 - II.队列的最大值(中等) 单调队列

剑指Offer 59 - I.滑动窗口的最大值 (困难)单调队列