83. Remove Duplicates from Sorted List

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.

//和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;

    }

 

}

posted @ 2019-04-09 16:27  MarkLeeBYR  阅读(102)  评论(0)    收藏  举报