LeetCode 删除排序链表中的重复元素 I (难度:简单)

版权声明:欢迎提问:[email protected] https://blog.csdn.net/include_heqile/article/details/81880584

https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/

我的解决方案:


class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null) return null;
        ListNode res= head;
        while(head.next!=null) {
            while(head.val==head.next.val) {
                head.next=head.next.next;
                if(head.next==null)
                    return res;
            }
            head=head.next;
        }
        return res;
    }
}

在我提交的时候,我的代码是耗时最短的

猜你喜欢

转载自blog.csdn.net/include_heqile/article/details/81880584