LeetCode83--删除排序链表中的重复元素

 1 '''
 2 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
 3 示例 1: 输入: 1->1->2 输出: 1->2
 4 示例 2: 输入: 1->1->2->3->3 输出: 1->2->3
 5 '''
 6 # 关键点:排序链表
 7 
 8 
 9 class ListNode:
10     def __init__(self, x):
11         self.val = x
12         self.next = None
13 
14 
15 class Solution:
16     def deleteDuplicates(self, head):
17         """
18         :type head: ListNode
19         :rtype: ListNode
20         """
21         cur = head
22         while cur is not None and cur.next is not None:
23             if cur.val == cur.next.val:
24                 cur.next = cur.next.next
25             else:
26                 cur = cur.next
27         return head
28 
29 
30 if __name__ == '__main__':
31     list1 = [1, 1, 2]
32     l1 = ListNode(1)
33     l1.next = ListNode(1)
34     l1.next.next = ListNode(2)
35     ret = Solution().deleteDuplicates(l1)
36     print(ret)

猜你喜欢

转载自www.cnblogs.com/Knight-of-Dulcinea/p/10059855.html