Sublist Search (Search a linked list in another list)
Last Updated :
07 May, 2023
Given two linked lists, the task is to check whether the first list is present in 2nd list or not.
Examples:
Input: list1 = 10->20
list2 = 5->10->20
Output : LIST FOUND
Input: list1 = 1->2->3->4
list2 = 1->2->1->2->3->4
Output: LIST FOUND
Input: list1 = 1->2->3->4
list2 = 1->2->2->1->2->3
Output: LIST NOT FOUND
Approach 1:
- Take first node of second list.
- Start matching the first list from this first node.
- If whole lists match return true.
- Else break and take first list to the first node again.
- And take second list to its second node.
- Repeat these steps until any of linked lists becomes empty.
- If first list becomes empty then list found else not.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node* next;
};
bool 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 ;
}
void printList(Node* node)
{
while (node != NULL)
{
printf ( "%d " , node->data);
node = node->next;
}
}
Node *newNode( int key)
{
Node *temp = new Node;
temp-> data= key;
temp->next = NULL;
return temp;
}
int main()
{
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);
findList(a,b) ? cout << "LIST FOUND" :
cout << "LIST NOT FOUND" ;
return 0;
}
|
Java
import java.util.*;
public 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" );
}
}
|
Python3
class Node:
def __init__( self , value = 0 ):
self .value = value
self . next = None
def findList(first, second):
if not first and not second:
return True
if not first or not second:
return False
ptr1 = first
ptr2 = second
while ptr2:
ptr2 = second
while ptr1:
if not ptr2:
return False
elif ptr1.value = = ptr2.value:
ptr1 = ptr1. next
ptr2 = ptr2. next
else :
break
if not ptr1:
return True
ptr1 = first
second = second. next
return False
node_a = Node( 1 )
node_a. next = Node( 2 )
node_a. next . next = Node( 3 )
node_a. next . next . next = Node( 4 )
node_b = Node( 1 )
node_b. next = Node( 2 )
node_b. next . next = Node( 1 )
node_b. next . next . next = Node( 2 )
node_b. next . next . next . next = Node( 3 )
node_b. next . next . next . next . next = Node( 4 )
if findList(node_a, node_b):
print ( "LIST FOUND" )
else :
print ( "LIST NOT FOUND" )
|
C#
using System;
class GFG
{
class Node
{
public int data;
public 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 )
{
Console.Write( "{0} " , 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 )
Console.Write( "LIST FOUND" );
else
Console.Write( "LIST NOT FOUND" );
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function findList(first, second) {
var 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 ;
}
function printList(node) {
while (node != null ) {
document.write( "%d " , node.data);
node = node.next;
}
}
function newNode(key) {
var temp = new Node();
temp.data = key;
temp.next = null ;
return temp;
}
var a = newNode(1);
a.next = newNode(2);
a.next.next = newNode(3);
a.next.next.next = newNode(4);
var 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 )
document.write( "LIST FOUND" );
else
document.write( "LIST NOT FOUND" );
</script>
|
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)
Optimization:
Above code can be optimized by using extra space i.e. storing the list into two strings and applying KMP algorithm.
Approach 2: Using recursion
Time complexity :- O(n*m)
The time complexity of this implementation is O(n*m), where n is the length of the main list and m is the length of the sublist. This is because in the worst case, we may have to iterate through the entire main list for each node in the sublist.
Space complexity :- O(m)
The space complexity of this implementation is O(m), where m is the length of the sublist. This is because we are only keeping track of one node at a time from the sublist, so the space required is proportional to the length of the sublist.
- Define a Node class with an int data member and a Node* pointer to the next node in the linked list.
- Define a recursive isSublist function that takes two Node* pointers as parameters: list (the main list) and sublist (the sublist to search for in the main list).
- In the isSublist function, handle the base cases:
a. If sublist is nullptr, i.e., the end of the sublist has been reached, return true because the sublist has been found.
b. If list is nullptr, i.e., the end of the main list has been reached but the sublist has not been found, return false.
- In the recursive case of the isSublist function:
a. If the data member of the current list node is equal to the data member of the current sublist node, recursively call isSublist with the next nodes in both lists (list->next and sublist->next).
b. If the data member of the current list node is not equal to the data member of the current sublist node, recursively call isSublist with the next node in the main list (list->next) and the entire sublist (sublist).
- In the main function:
a. Create a main linked list called list with 6 nodes, each with an int data member.
b. Create a sublist called sublist with 4 nodes, each with an int data member.
c. Call the isSublist function with list and sublist as arguments.
d. If the isSublist function returns true, print “LIST FOUND”. Otherwise, print “LIST NOT FOUND”.
Return 0 to exit the program.
C++
#include <iostream>
using namespace std;
class Node {
public :
int data;
Node* next;
Node( int data)
{
this ->data = data;
next = nullptr;
}
};
bool isSublist(Node* list, Node* sublist)
{
if (sublist == nullptr) {
return true ;
}
if (list == nullptr) {
return false ;
}
if (list->data == sublist->data) {
return isSublist(list->next, sublist->next);
}
else {
return isSublist(list->next, sublist);
}
}
int main()
{
Node* list = new Node(1);
list->next = new Node(2);
list->next->next = new Node(1);
list->next->next->next = new Node(2);
list->next->next->next->next = new Node(3);
list->next->next->next->next->next = new Node(4);
Node* sublist = new Node(1);
sublist->next = new Node(2);
sublist->next->next = new Node(3);
sublist->next->next->next = new Node(4);
if (isSublist(list, sublist)) {
cout << "LIST FOUND" << endl;
}
else {
cout << "LIST NOT FOUND" << endl;
}
return 0;
}
|
Java
class Node {
public int data;
public Node next;
public Node( int data)
{
this .data = data;
next = null ;
}
}
public class Main {
public static boolean isSublist(Node list, Node sublist)
{
if (sublist == null ) {
return true ;
}
if (list == null ) {
return false ;
}
if (list.data == sublist.data) {
return isSublist(list.next, sublist.next);
}
else {
return isSublist(list.next, sublist);
}
}
public static void main(String[] args)
{
Node list = new Node( 1 );
list.next = new Node( 2 );
list.next.next = new Node( 1 );
list.next.next.next = new Node( 2 );
list.next.next.next.next = new Node( 3 );
list.next.next.next.next.next = new Node( 4 );
Node sublist = new Node( 1 );
sublist.next = new Node( 2 );
sublist.next.next = new Node( 3 );
sublist.next.next.next = new Node( 4 );
if (isSublist(list, sublist)) {
System.out.println( "LIST FOUND" );
}
else {
System.out.println( "LIST NOT FOUND" );
}
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def isSublist( list , sublist):
if sublist is None :
return True
if list is None :
return False
if list .data = = sublist.data:
return isSublist( list . next , sublist. next )
else :
return isSublist( list . next , sublist)
list = Node( 1 )
list . next = Node( 2 )
list . next . next = Node( 1 )
list . next . next . next = Node( 2 )
list . next . next . next . next = Node( 3 )
list . next . next . next . next . next = Node( 4 )
sublist = Node( 1 )
sublist. next = Node( 2 )
sublist. next . next = Node( 3 )
sublist. next . next . next = Node( 4 )
if isSublist( list , sublist):
print ( "LIST FOUND" )
else :
print ( "LIST NOT FOUND" )
|
C#
using System;
class Node {
public int data;
public Node next;
public Node( int data) {
this .data = data;
this .next = null ;
}
}
class Sublist {
public static bool IsSublist(Node list, Node sublist) {
if (sublist == null ) {
return true ;
}
if (list == null ) {
return false ;
}
if (list.data == sublist.data) {
return IsSublist(list.next, sublist.next);
} else {
return IsSublist(list.next, sublist);
}
}
}
class Program {
static void Main( string [] args) {
Node list = new Node(1);
list.next = new Node(2);
list.next.next = new Node(1);
list.next.next.next = new Node(2);
list.next.next.next.next = new Node(3);
list.next.next.next.next.next = new Node(4);
Node sublist = new Node(1);
sublist.next = new Node(2);
sublist.next.next = new Node(3);
sublist.next.next.next = new Node(4);
if (Sublist.IsSublist(list, sublist)) {
Console.WriteLine( "LIST FOUND" );
} else {
Console.WriteLine( "LIST NOT FOUND" );
}
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
class Sublist {
static isSublist(list, sublist) {
if (sublist == null ) {
return true ;
}
if (list == null ) {
return false ;
}
if (list.data == sublist.data) {
return Sublist.isSublist(list.next, sublist.next);
} else {
return Sublist.isSublist(list.next, sublist);
}
}
}
let list = new Node(1);
list.next = new Node(2);
list.next.next = new Node(1);
list.next.next.next = new Node(2);
list.next.next.next.next = new Node(3);
list.next.next.next.next.next = new Node(4);
let sublist = new Node(1);
sublist.next = new Node(2);
sublist.next.next = new Node(3);
sublist.next.next.next = new Node(4);
if (Sublist.isSublist(list, sublist)) {
console.log( "LIST FOUND" );
} else {
console.log( "LIST NOT FOUND" );
}
|
Time Complexity: O(m*n)
Auxiliary Space: O(m)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...