-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveDupFromSortedLL.py
More file actions
49 lines (36 loc) · 1.16 KB
/
removeDupFromSortedLL.py
File metadata and controls
49 lines (36 loc) · 1.16 KB
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
# https://leetcode.com/problems/remove-duplicates-from-sorted-list/?envType=study-plan&id=data-structure-i
# 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: Optional[ListNode]) -> Optional[ListNode]:
# temp = head
# l = []
# while temp:
# if temp.val not in l:
# l.append(temp.val)
# temp = temp.next
# dummy = ll = ListNode(0)
# for i in l:
# ll.next = ListNode(i)
# ll = ll.next
# return dummy.next
# OR
# temp = head
# while temp:
# temp2 = temp
# while temp2:
# if temp.val == temp2.val:
# temp.next = temp2.next
# temp2 = temp2.next
# temp = temp.next
# return head
# OR
curr = head
while curr:
while curr.next and curr.next.val == curr.val:
curr.next = curr.next.next
curr = curr.next
return head