-
Notifications
You must be signed in to change notification settings - Fork 368
/
sublist_search.py
135 lines (102 loc) · 3.78 KB
/
sublist_search.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Path: Python\Searching\sublist_search.c
# Python program to check whether the first list is present in 2nd list or not, when two linked lists are given.
# Time-Complexity: O(M*N), where M is the number of nodes in the second list and N in the first.
# Auxiliary-Space: O(1).
# Class representing a node in the linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Class representing the linked list
class SingleLinkedList:
def __init__(self):
self.head = None
# Function to insert a node at the end of the linked list
def insertAtEnd(self, data):
newNode = Node(data)
# If the list is empty, make the new node the head
if self.head is None:
self.head = newNode
return
# Traverse to the last node
temp = self.head
while temp.next:
temp = temp.next
# Insert the new node at the end
temp.next = newNode
# Function to display the linked list
def displayList(self):
temp = self.head
if temp is None:
print("NULL", end="")
while temp:
print(temp.data, end=" ")
temp = temp.next
print()
# Returns true if first list is present in second list else false
def isSublist(self, list1, list2):
# If list1 and list2 are null (Empty), list1 is sublist of list2
if list1.head is None and list2.head is None:
return True
# Else, if list1 is null and list2 is not, return false
if list1.head is None or (list1.head is not None and list2.head is None):
return False
# current1 is to traverse in list1 and current2 in list2
current1 = list1.head
current2 = list2.head
# Traverse the second list by picking nodes one by one
while list2.head:
# Initialize current2 pointer with the current node of the second list
current2 = list2.head
# Start matching the first list with the second list
while current1:
# If the second list becomes empty and the first list does not, return false
if current2 is None:
return False
# If the data part is the same, go to the next nodes of both lists
elif current1.data == current2.data:
current1 = current1.next
current2 = current2.next
# If they are not equal, break the loop
else:
break
# Returns True, If list1 is traversed completely (till the end)
# Indicating that list1 is found
if current1 is None:
return True
# Initialize current1 pointer with first again
current1 = list1.head
# Move to the next node of the second list
list2.head = list2.head.next
return False
# Program starts here
# Create the first linked list
list1 = SingleLinkedList()
# Size of list1
N = int(input("No of Nodes in the first list: "))
for i in range(N):
data = int(input("data {}: ".format(i + 1)))
list1.insertAtEnd(data)
print()
# Create the second linked list
list2 = SingleLinkedList()
# Size of list2
M = int(input("No of Nodes in the second list: "))
for i in range(M):
data = int(input("data {}: ".format(i + 1)))
list2.insertAtEnd(data)
print()
# Display the first linked list
print("List 1: ", end="")
list1.displayList()
# Display the second linked list
print("List 2: ", end="")
list2.displayList()
print()
# Check if the first list is a sublist of the second list
result = list1.isSublist(list1, list2)
print("Is the first list a sublist of the second list? ", end="")
if result:
print("True")
else:
print("False")