forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSublist_search_python
100 lines (80 loc) · 2.07 KB
/
Sublist_search_python
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Python3 program to find a list in second list
class Node:
def __init__(self, value = 0):
self.value = value
self.next = None
# Returns true if first list is
# present in second list
def findList(first, second):
# If both linked lists are empty/None,
# return True
if not first and not second:
return True
# If ONLY one of them is empty,
# return False
if not first or not second:
return False
ptr1 = first
ptr2 = second
# Traverse the second LL by
# picking nodes one by one
while ptr2:
# Initialize 'ptr2' with current
# node of 'second'
ptr2 = second
# Start matching first LL
# with second LL
while ptr1:
# If second LL become empty and
# first not, return False,
# since first LL has not been
# traversed completely
if not ptr2:
return False
# If value of both nodes from both
# LLs are equal, increment pointers
# for both LLs so that next value
# can be matched
elif ptr1.value == ptr2.value:
ptr1 = ptr1.next
ptr2 = ptr2.next
# If a single mismatch is found
# OR ptr1 is None/empty,break out
# of the while loop and do some checks
else:
break
# check 1 :
# If 'ptr1' is None/empty,that means
# the 'first LL' has been completely
# traversed and matched so return True
if not ptr1:
return True
# If check 1 fails, that means, some
# items for 'first' LL are still yet
# to be matched, so start again by
# bringing back the 'ptr1' to point
# to 1st node of 'first' LL
ptr1 = first
# And increment second node element to next
second = second.next
return False
# Driver Code
# Let us create two linked lists to
# test the above functions.
# Created lists would be be
# node_a: 1->2->3->4
# node_b: 1->2->1->2->3->4
node_a = Node(1)
node_a.next = Node(2)
node_a.next.next = Node(3)
node_a.next.next.next = Node(4)
node_b = Node(1)
node_b.next = Node(2)
node_b.next.next = Node(1)
node_b.next.next.next = Node(2)
node_b.next.next.next.next = Node(3)
node_b.next.next.next.next.next = Node(4)
if findList(node_a, node_b):
print("LIST FOUND")
else:
print("LIST NOT FOUND")