-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path83_RemoveDuplicatesfromSortedList.py
48 lines (42 loc) · 1.27 KB
/
83_RemoveDuplicatesfromSortedList.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
# https://leetcode.com/problems/remove-duplicates-from-sorted-list/submissions/
# 2021 12.03
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
# # v1
# returnHead=head
# while head:
# mid=head
# while mid.next and mid.val==mid.next.val:
# mid=mid.next
# head.next=mid.next
# head=head.next
# return returnHead
# # v2 Recursion
# if head == None:
# return head
# while head.next!=None and head.next.val==head.val:
# head=head.next
# head.next=self.deleteDuplicates(head.next)
# return head
# v2.1 copied Recursion
if head == None or head.next==None:
return head
head.next=self.deleteDuplicates(head.next)
return head.next if head.val == head.next.val else head
l=[10,2,2,2,3,4,5,6,6,6,6,6,7]
Ln=ListNode(l[0])
lnhead=Ln
for i in range(1,len(l)):
Ln.next=ListNode(l[i])
Ln=Ln.next
# print(lnhead.val)
s=Solution()
ret=s.deleteDuplicates(lnhead)
while ret:
print(ret.val)
ret=ret.next