Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

//和203有所不同,203第一个节点也可能被remove所以要借助dummy。这个题不需要dummy 

 

public class Solution {

    public ListNode deleteDuplicates(ListNode head) {

        ListNode list = head;

        if (head == null || head.next == null) {

            return head;

        }

        while (head != null && head.next != null){

            if (head.val == head.next.val) {

                head.next = head.next.next;

            } else {

                head = head.next;

            }

        }

        return list;

    }

 

}

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄