-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
51 lines (45 loc) · 1.63 KB
/
answer.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
#!/usr/bin/env python
#-------------------------------------------------------------------------------
# My Solution
#-------------------------------------------------------------------------------
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
unique = []
# Initialize prev and curr - return if 1 or 0 element list
if head and head.next:
prev = head
curr = head.next
unique.append(prev.val)
else:
return head
# Iterate through list adding each unique value to the list
while curr:
if curr.val not in unique:
unique.append(curr.val)
curr = curr.next
prev = prev.next
else:
curr = curr.next
prev.next = curr
return head
#-------------------------------------------------------------------------------
# Optimized Solution
#-------------------------------------------------------------------------------
def deleteDuplicates2(self, head):
cur = head
while cur:
while cur.next and cur.next.val == cur.val:
cur.next = cur.next.next # skip duplicated node
cur = cur.next # not duplicate of current node, move to next node
return head
#-------------------------------------------------------------------------------
# Testing