-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge 2 list.py
64 lines (55 loc) · 1.48 KB
/
merge 2 list.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
52
53
54
55
56
57
58
59
60
61
62
63
64
class Node:
def __init__(self,data):
self.data=data
self.next=None
class LinkedList:
def __init__(self):
self.head=None
self.lastnode=None
def at_end(self,data):
if self.head is None:
self.head=Node(data)
self.lastnode=self.head
else:
temp=Node(data)
self.lastnode.next=temp
self.lastnode=temp
def merge(self,obj1):
self.lastnode.next=obj1.head
def clone(self,obj):
self.head=Node(obj.head.data)
self.lastnode=self.head
current=obj.head
current=current.next
while current is not None:
temp=Node(current.data)
self.lastnode.next=temp
self.lastnode=temp
current=current.next
def display(self):
if self.head is None:
print("List is empty ")
else:
current=self.head
while current is not None:
print(current.data)
current=current.next
obj=LinkedList()
obj1=LinkedList()
n=int(input("How many elements you wants to enter :"))
for i in range(n):
data=int(input("Enter data "))
obj.at_end(data)
obj.display()
n=int(input("How many elements you wants to enter :"))
for i in range(n):
data=int(input("Enter data "))
obj1.at_end(data)
obj1.display()
print("After Merging :")
obj.merge(obj1)
obj.display()
print("After Cloning :")
obj2=LinkedList()
obj2.clone(obj)
obj2.display()