第二周(链表专题)

Acwing打卡(第二周)

Acwing活动地址

本周为链表题目

19. 删除链表的倒数第N个节点
  • 两次遍历,第一次确定链表长度,第二次找到待删节点前驱节点
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        // 有可能删除到首节点的通常会用一个虚拟头结点
        ListNode dummy = new ListNode(Integer.MIN_VALUE);
        dummy.next = head;
        ListNode cur = head;
        int size = 0;
        while(cur != null){
            size ++;
            cur = cur.next;
        }
        cur = dummy;
        for(int i = 1; i <= size - n; i ++){
            cur = cur.next;
        }
        cur.next = cur.next.next;
        return dummy.next;
    }
}
  • 一次遍历,设置两个节点first和second
  • first先走n+1,然后first和second一起走直到fast为空
  • second下一个为待删元素
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(Integer.MIN_VALUE);
        dummy.next = head;
        ListNode first = dummy;
        ListNode second = dummy;
        for(int i = 0; i <= n; i ++){
            first = first.next;
        }
        while(first != null){
            first = first.next;
            second = second.next;
        }
        second.next = second.next.next;
        return dummy.next;
    }
}
237. 删除链表中的节点
  • 因为链表是单链表不能找到前驱节点,所以可以将下个节点的值复制到当前节点,然后删除下一个节点
class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }
}
83. 删除排序链表中的重复元素
  • 当前节点等于下一个节点时就把下一个节点删除
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode cur = head;
        while(cur != null && cur.next != null){
            if(cur.val == cur.next.val){
                cur.next = cur.next.next;
            }else{
                cur = cur.next;
            }
        }
        return head;
    }
}
61. 旋转链表
  • 设置两个节点first和second
  • first先走k步,然后first和second一起走直到first走到最后一个节点
  • second节点就是分界点,把其断开,然后first接到头节点即可
class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if(head == null){
            return head;
        }
        ListNode cur = head;
        
        int size = 0;
        while(cur != null){
            size ++;
            cur = cur.next;
        }
        k %= size; // k可能很大
        if(k == 0){
            return head;
        }
        ListNode first = head;
        ListNode second = head;
        
        while(k -- > 0&& first != null){
            first = first.next;
        }
        while(first.next != null){
            first = first.next;
            second = second.next;
        }
        ListNode ret = second.next;
        second.next = null;
        first.next = head;
        return ret;
    }
}
24. 两两交换链表中的节点
  • 模拟一遍操作即可
class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(99999);
        dummy.next = head;
        ListNode cur = dummy;
        
        while(cur.next != null && cur.next.next != null){
            ListNode a = cur.next, b = a.next;
            cur.next = b;
            a.next = b.next;
            b.next = a;
            cur = a;
        }
        return dummy.next;
    }
}

你可能感兴趣的:(第二周(链表专题))