forked from deutranium/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sublistSearch.java
67 lines (52 loc) · 1.16 KB
/
sublistSearch.java
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
package searchingAlgo.sublistSearch;
class Node {
int data;
String sdata;
Node next;
Node(int d) {
data = d;
next = null;
}
}
public class sublistSearch {
public static boolean sublistSearch(Node subList, Node list) {
if(subList == null && list == null) {
return true;
}else if (subList == null || (subList != null && list == null)) {
return false;
}
Node curr1 = subList;
Node curr2 = list;
while(list != null) {
curr2 = list;
while(curr1 != null) {
if (curr2 == null) {
return false;
}
if (curr1.data == curr2.data) {
curr1 = curr1.next;
curr2 = curr2.next;
}else {
break;
}
}
if (curr1 == null) {
return true;
}
curr1 = subList;
list = list.next;
}
return false;
}
public static void main(String[] args) {
Node subList1 = new Node(1);
subList1.next = new Node(2);
Node subList2 = new Node(2);
subList2.next = new Node(3);
Node list = new Node(1);
list.next = new Node(2);
list.next.next = new Node(4);
System.out.println(sublistSearch(subList1,list));
System.out.println(sublistSearch(subList2,list));
}
}