-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompare two linked list solution.py
More file actions
77 lines (53 loc) · 1.33 KB
/
Copy pathCompare two linked list solution.py
File metadata and controls
77 lines (53 loc) · 1.33 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def compare(temp1, temp2):
head1=temp1
head2=temp2
x=[]
y=[]
#return 1/-1/0
while(temp1):
x.append(temp1.data)
temp1=temp1.next
while(temp2):
y.append(temp2.data)
temp2=temp2.next
if (x==y):
return 0
elif (x>y):
return 1
else:
return -1
class Node:
def __init__(self,data):
self.data=data
self.next=None
class Llist:
def __init__(self):
self.head=None
def insert(self,data,tail):
node=Node(data)
if not self.head:
self.head=node
return node
tail.next=node
return node
def printList(head):
while head:
print(head.data,end=' ')
head=head.next
if __name__ == '__main__':
t=int(input())
for tcs in range(t):
n1=int(input())
arr1=input().split()
ll1=Llist()
tail=None
for nodeData in arr1:
tail=ll1.insert(nodeData,tail)
n2=int(input())
arr2=input().split()
ll2=Llist()
tail=None
for nodeData in arr2:
tail=ll2.insert(nodeData,tail)
print(compare(ll1.head,ll2.head))
# } Driver Code Ends