首页 > 其他分享 >leetcode-83-easy

leetcode-83-easy

时间:2022-10-13 18:11:36浏览次数:40  
标签:head right val next easy 83 null leetcode left

Remove Duplicates from Sorted List
思路一: 双指针,左指针记录链表最后有效位置,右指针向前扫描,遇到不重复的值,加到左指针后面,双指针依次向前

public ListNode deleteDuplicates(ListNode head) {
    if (head == null || head.next == null) return head;
    ListNode left = head;
    ListNode right = head;

    while (right != null) {
        if (left.val != right.val) {
            left = left.next;
            left.val = right.val;
        }

        right = right.next;
    }

    if (left.next != null) {
        left.next = null;
    }
    return head;
}

标签:head,right,val,next,easy,83,null,leetcode,left
From: https://www.cnblogs.com/iyiluo/p/16789173.html

相关文章