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.

 

 1 # Definition for singly-linked list.
 2 # class ListNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6 
 7 class Solution(object):
 8     def deleteDuplicates(self, head):
 9         """
10         :type head: ListNode
11         :rtype: ListNode
12         """
13 
14         cur = head
15         while cur:
16             while cur.next and cur.next.val == cur.val:
17                 cur.next = cur.next.next     # skip duplicated node
18             cur = cur.next     # not duplicate of current node, move to next node
19         return head

 

posted on 2017-03-15 14:52  Ci_pea  阅读(106)  评论(0编辑  收藏  举报