-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path083_remove-duplicates-from-sorted-list.py
67 lines (58 loc) · 1.58 KB
/
083_remove-duplicates-from-sorted-list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#@author: rye
#@time: 2019/3/27
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None:
return head
dummy = ListNode(-1)
dummy.next = head
q = dummy
if q.next:
q = q.next
while q and q.next:
if q.val == q.next.val:
q.next = q.next.next
else:
q = q.next
return dummy.next.val
# while q.next:
# q = q.next
# while q.next:
# if q.val == q.next.val:
# q = q.next
# else:
# p = q
# 大佬的代码
class Solution1(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
curr = head
while curr:
while curr.next:
if curr.val == curr.next.val:
curr.next = curr.next.next
else:
break
curr = curr.next
return head
if __name__ == '__main__':
l1 = ListNode(1)
l1.next = ListNode(1)
l1.next.next = ListNode(1)
# l1.next.next.next = ListNode(3)
# l1.next.next.next.next = ListNode(3)
print(Solution().deleteDuplicates(l1))