- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathSublistSearch.java
More file actions
Latest commit
168 lines (134 loc) · 5.22 KB
/
Copy pathSublistSearch.java
File metadata and controls
168 lines (134 loc) · 5.22 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Path: C\Searching\sublist_search.c
// C 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).
importjava.util.Scanner;
// Main class
publicclassSublistSearch {
// Class representing a node in the linked list
staticclassNode {
intdata;
Nodenext;
Node(intdata) {
this.data = data;
this.next = null;
}
}
// Class representing the linked list
staticclassSingleLinkedList {
Nodehead;
// Function to insert a node at the end of the linked list
voidinsertAtEnd(intdata) {
NodenewNode = newNode(data);
// If the list is empty, make the new node the head
if (head == null) {
head = newNode;
return;
}
// Traverse to the last node
Nodetemp = head;
while (temp.next != null) {
temp = temp.next;
}
// Insert the new node at the end
temp.next = newNode;
}
// Function to display the linked list
voiddisplayList() {
Nodetemp = head;
if (temp == null)
System.out.print("NULL");
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
// Returns true if first list is present in second list else false
booleanisSublist(SingleLinkedListlist1, SingleLinkedListlist2) {
// If list1 and list2 are null (Empty), list1 is sublist of list2
if (list1.head == null && list2.head == null) {
returntrue;
}
// Else, if list1 is null and list2 is not, return false
if (list1.head == null) {
returnfalse;
}
// current1 is to traverse in list1 and current2 in list2
Nodecurrent1 = list1.head;
Nodecurrent2 = list2.head;
// Traverse the second list by picking nodes one by one
while (list2.head != null) {
// Initialize current2 pointer with the current node of the list2
current2 = list2.head;
// Start matching the first list with the second list
while (current1 != null) {
// If the second list becomes empty and the first list does not, return false
if (current2 == null) {
returnfalse;
}
// If the data part is the same, go to the next nodes of both lists
elseif (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 == null) {
returntrue;
}
// Initialize current1 pointer with first again
current1 = list1.head;
// Move to the next node of the second list
list2.head = list2.head.next;
}
returnfalse;
}
}
publicstaticvoidmain(String[] args) {
// Create the first linked list
SingleLinkedListlist1 = newSingleLinkedList();
Scannerscanner = newScanner(System.in);
// Size of list1
System.out.print("No of Nodes in the first list: ");
intN = scanner.nextInt();
for (inti = 0; i < N; i++) {
System.out.print("data " + (i + 1) + ": ");
intdata = scanner.nextInt();
list1.insertAtEnd(data);
}
System.out.println();
// Create the second linked list
SingleLinkedListlist2 = newSingleLinkedList();
// Size of list2
System.out.print("No of Nodes in the second list: ");
intM = scanner.nextInt();
for (inti = 0; i < M; i++) {
System.out.print("data " + (i + 1) + ": ");
intdata = scanner.nextInt();
list2.insertAtEnd(data);
}
System.out.println();
// Display the first linked list
System.out.print("List 1: ");
list1.displayList();
// Display the second linked list
System.out.print("List 2: ");
list2.displayList();
System.out.println();
// Check if the first list is a sublist of the second list
booleanresult = list1.isSublist(list1, list2);
System.out.print("Is the first list a sublist of the second list? ");
if (result) {
System.out.println("True\n");
} else {
System.out.println("False\n");
}
scanner.close();
}
}