首页 > 编程语言 > 详细

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

时间:2018-12-03 18:23:29      阅读:147      评论:0      收藏:0      [点我收藏+]

 

 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)

 

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

原文:https://www.cnblogs.com/Knight-of-Dulcinea/p/10059855.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!