Skip to content

Commit

Permalink
Solved sublist search algo rithm
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabh12nxf committed Feb 19, 2025
1 parent 2728bc6 commit d4df39b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
Binary file added src/Algorithms/Node.class
Binary file not shown.
Binary file added src/Algorithms/SublistSearchAlgorithm.class
Binary file not shown.
70 changes: 70 additions & 0 deletions src/Algorithms/SublistSearchAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class Node {
int data;
Node next;

Node(int data) {
this.data = data;
this.next = null;
}
}
public class SublistSearchAlgorithm {

public static boolean isSublist(Node list1, Node list2) {
if (list2 == null) {
return true;
}
if (list1 == null) {
return false;
}

Node current1 = list1;
Node current2 = list2;

while (current1 != null) {
Node temp1 = current1;
Node temp2 = current2;


while (temp2 != null) {
if (temp1 == null || temp1.data != temp2.data) {
break;
}
temp1 = temp1.next;
temp2 = temp2.next;
}
if (temp2 == null) {
return true;
}

current1 = current1.next;
}

return false;
}

public static Node createLinkedList(int[] arr) {
if (arr.length == 0) {
return null;
}

Node head = new Node(arr[0]);
Node current = head;

for (int i = 1; i < arr.length; i++) {
current.next = new Node(arr[i]);
current = current.next;
}

return head;
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {3, 4};

Node list1 = createLinkedList(arr1);
Node list2 = createLinkedList(arr2);

boolean result = isSublist(list1, list2);
System.out.println("Is list2 a sublist of list1? " + result);
}
}

0 comments on commit d4df39b

Please sign in to comment.