-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsublist-search.java
104 lines (71 loc) · 1.3 KB
/
sublist-search.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
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
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static boolean findList(Node first,
Node second)
{
Node ptr1 = first, ptr2 = second;
if (first == null && second == null)
return true;
if (first == null ||
(first != null && second == null))
return false;
while (second != null)
{
ptr2 = second;
while (ptr1 != null)
{
if (ptr2 == null)
return false;
else if (ptr1.data == ptr2.data)
{
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
else break;
}
if (ptr1 == null)
return true;
ptr1 = first;
second = second.next;
}
return false;
}
static void printList(Node node)
{
while (node != null)
{
System.out.printf("%d ", node.data);
node = node.next;
}
}
static Node newNode(int key)
{
Node temp = new Node();
temp.data= key;
temp.next = null;
return temp;
}
public static void main(String[] args)
{
Node a = newNode(1);
a.next = newNode(2);
a.next.next = newNode(3);
a.next.next.next = newNode(4);
Node b = newNode(1);
b.next = newNode(2);
b.next.next = newNode(1);
b.next.next.next = newNode(2);
b.next.next.next.next = newNode(3);
b.next.next.next.next.next = newNode(4);
if(findList(a, b) == true)
System.out.println("LIST FOUND");
else
System.out.println("LIST NOT FOUND");
}
}