Loading...

双指针-删除有序链表中重复的元素-II

在这里插入图片描述

求解代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public ListNode deleteDuplicates (ListNode head) {
    // 空链表 或 单节点链表,无重复节点,直接返回
    if(head == null || head.next == null){
        return head;
    }
    // 虚拟头节点
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode cur = head;   // 游标指针,遍历链表找重复节点
    ListNode pre = dummy;  // 前驱指针,锚定无重复的有效节点
    
    while(cur != null){
        // 找到连续重复节点的最后一个节点
        while(cur.next != null && cur.val == cur.next.val){
            cur = cur.next;
        }
        
        if(pre.next == cur){
            pre = pre.next; // 从 pre 到 cur,中间没有跳过节点
        }else{
            pre.next = cur.next; // 从 pre 到 cur,中间跳过了节点
        }
        cur = cur.next; // 游标指针后移,继续遍历
    }
    // 返回新链表的有效头节点
    return dummy.next;
}

说明:

pre是永远站在「已经确认无重复的最后一个节点」的位置。

最后更新于 2026-04-05 17:35:33
Code Road Record