forked from geekquad/AlgoBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsublist-search.py
More file actions
76 lines (62 loc) · 1.82 KB
/
sublist-search.py
File metadata and controls
76 lines (62 loc) · 1.82 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
# SUBLIST SEARCH
#Checks whether the first list is present in second list or not.
#Taking fisrt node of second list
class Nodes:
def __init__(self, value = 0):
self.value = value
self.next = None
#Returns TRUE if fisrt list is in the second list.
def findList(first,second):
if not first or not second:
return False
if not first and not second:
return True
p1 = first
p2 = second
# Traversing the second LL
# Checking nodes from both lists
while p2:
#Initializing p2 with current node of second LL
p2 = second
#Starting our matching
while p1:
# If second LL become empty and
# first not, return False,
if not p2:
return False
#if nodes match, increase the pointers
#for checking the next nodes
elif p1.value == p2.value:
p1 = p1.next
p2 = p2.next
#If nodes don't match further or the
#p1 is None/empty
else:
break
#Checks
if not p1: #p1 is empty/None
return True #Completely traversed and matched
#If p1 is not empty/None
p1 = first #bringing back to fisrt
second = second.next #increament second node element
return False
#Driver Code
#Enter two linked list
# Example -
#node_a: 1-> 2-> 3-> 4
# node_b: 1->2->1->2->3->4
node_a = Nodes(1)
node_a.next = Nodes(2)
node_a.next.next = Nodes(3)
node_a.next.next.next = Nodes(4)
node_b = Nodes(1)
node_b.next = Nodes(2)
node_b.next.next = Nodes(1)
node_b.next.next.next = Nodes(2)
node_b.next.next.next.next = Nodes(3)
node_b.next.next.next.next.next = Nodes(4)
if findList(node_a, node_b):
print("LIST FOUND")
else:
print("LIST NOT FOUND")
#OUTPUT : LIST FOUND