Skip to content

Commit d71316b

Browse files
committed
Adding Efficient Solution for Problem - 83 - Remove Duplicate LinkedList
1 parent bf32e03 commit d71316b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 83
6+
## Problem Name: Remove Duplicates from Sorted List
7+
##===================================
8+
#
9+
#Given a sorted linked list, delete all duplicates such that each element appear only once.
10+
#
11+
#Example 1:
12+
#
13+
#Input: 1->1->2
14+
#Output: 1->2
15+
#
16+
#Example 2:
17+
#
18+
#Input: 1->1->2->3->3
19+
#Output: 1->2->3
20+
class Solution:
21+
def deleteDuplicates(self, head):
22+
tmp = head #Referencing head value to tmp
23+
24+
while tmp and tmp.next: #Loop till tmp and tmp.next is not empty
25+
if tmp.val == tmp.next.val: #Condition-check: if tmp.val and tmp.next.val is same
26+
tmp.next = tmp.next.next #We point to tmp.next.next
27+
else: #Condition-check: else condition
28+
tmp = tmp.next #We point to tmp.next
29+
return head #We'll return the head at the end.
30+
#Example:
31+
#head = [1, 1, 2]
32+
#tmp = head = [1, 1, 2]
33+
#While loop:
34+
#----------
35+
#tmp.val = 1 and tmp.next.val = 1
36+
#tmp.next = tmp.next.next = 1
37+
#----------
38+
#tmp.val = 1 and tmp.next.next = 2
39+
#----------
40+
#head = [1, 2]

0 commit comments

Comments
 (0)