# Given two linked list, you have to check whether the first one is present in 2nd one or not?

list1:    1->2->3->4->2->3
list2:    1->2->1->2->3->4->2->3->5->6

Output- 1

list1:    1->2->3->4->2->3
list2:    2->1->2->4->2->3->5->6

Output- 0

Data Can be duplicates/Order of the data matters...
-Given brutforce solution of O(m*n) but iterviewer was not happy with that.

Does the continuation of elements of the list to be checked matter?

example: if in the first example if list2 was 1->2->9->2->3->4->2->3.

does it still return 1?

Order should be same. Actually, it's a pattern search problem... Given a text of linked list, u have to find a pattern of linked list in it...

```#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

struct node
{
int data;
struct node *next;
};

int isEmpty(struct node *root)
{
return !root;
}
/* Given a reference (pointer to pointer) to the head of a list and
an int, inserts a new node on the front of the list. */
void push(struct node** head_ref, int new_data)
{
/* 1. allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct node));

/* 2. put in the data */
new_node->data = new_data;

/* 3. Make next of new node as head */

/* 4. move the head to point to the new node */
}

int pop(struct node** root)
{
if (isEmpty(*root))
return 0;
struct node* temp = *root;
*root = (*root)->next;
int popped = temp->data;
free(temp);

return popped;
}

void printList(struct node *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->next;
}
}

/* Drier program to test above functions*/
int main()
{

printf("\n Created first  Linked list is: ");

printf("\n Created second  Linked list is: ");

int ans;
while(true)
{
if(x==t)
{

}
else if(t==0)
{
ans=0;
break;

}
else if(x==0)
{
ans=1;
break;
}
else

}

return 0;
}
```

edited Sep 5, 2016
```   1  2  3  4  2  3
1  2  1  3  4  2  3  5  6

First one is not present in 2nd one. Order should be same. It can't handle duplicate elements

```

i m not able to understand what u want to ask

all the element of first linked list  are present in the second linked list

The elements are present but not in order. As mentioned above it is a pattern search. So the solution provided by you is not correct.

which order???

can u explain more about that

in my program i have use linked list so the order start from backward