Skip to content

Commit a7b662c

Browse files
More Linked Lists
1 parent 30b33b3 commit a7b662c

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Day 24: More Linked Lists.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Node:
2+
def __init__(self,data):
3+
self.data = data
4+
self.next = None
5+
class Solution:
6+
def insert(self,head,data):
7+
p = Node(data)
8+
if head==None:
9+
head=p
10+
elif head.next==None:
11+
head.next=p
12+
else:
13+
start=head
14+
while(start.next!=None):
15+
start=start.next
16+
start.next=p
17+
return head
18+
def display(self,head):
19+
current = head
20+
while current:
21+
print(current.data,end=' ')
22+
current = current.next
23+
24+
def removeDuplicates(self,head):
25+
#Write your code here
26+
node = head
27+
while(node!=None):
28+
nextnode = node.next
29+
data = node.data
30+
while(nextnode!=None):
31+
if(nextnode.data==data):
32+
node.next = nextnode.next
33+
nextnode = nextnode.next
34+
node = node.next
35+
return head
36+
37+
mylist= Solution()
38+
T=int(input())
39+
head=None
40+
for i in range(T):
41+
data=int(input())
42+
head=mylist.insert(head,data)
43+
head=mylist.removeDuplicates(head)
44+
mylist.display(head);

0 commit comments

Comments
 (0)