-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDeleteDuplicatedNode.py
41 lines (41 loc) · 1.18 KB
/
DeleteDuplicatedNode.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
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplication(self, head):
# write code here
if head == None or head.next == None:
return head
p = ListNode(-1)
p.next = head
prev = p
cur = head
while cur and cur.next:
if cur.val == cur.next.val:
while cur.next and cur.val == cur.next.val:
cur = cur.next
prev.next = cur.next
else:
prev = cur
cur = cur.next
return p.next
## Solution 2:
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplication(self, head):
# write code here
if head == None or head.next == None:
return head
if head.val == head.next.val:
while head.next and head.val == head.next.val:
head = head.next
head = self.deleteDuplication(head.next)
else:
head.next = self.deleteDuplication(head.next)
return head