Write a C function that searches a given key ‘x’ in a given singly linked list. The function should return true if x is present in linked list and false otherwise.

   bool search(Node *head, int x)

For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then function should return false. If key to be searched is 14, then the function should return true.

Iterative Solution

2) Initialize a node pointer, current = head.
3) Do following while current is not NULL
    a) current->key is equal to the key being searched return true.
    b) current = current->next
4) Return false

Java Programming:

// Iterative Java program to search an element
// in linked list

//Node class
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}

//Linked list class
class LinkedList
{
Node head; //Head of list

//Inserts a new node at the front of the list
public void push(int new_data)
{
//Allocate new node and putting data
Node new_node = new Node(new_data);

//Make next of new node as head
new_node.next = head;

//Move the head to point to new Node
head = new_node;
}

//Checks whether the value x is present in linked list
public boolean search(Node head, int x)
{
Node current = head; //Initialize current
while (current != null)
{
if (current.data == x)
return true; //data found
current = current.next;
}
return false; //data not found
}

//Driver function to test the above functions
public static void main(String args[])
{

//Start with the empty list
LinkedList llist = new LinkedList();

/*Use push() to construct below list
14->21->11->30->10 */
llist.push(10);
llist.push(30);
llist.push(11);
llist.push(21);
llist.push(14);

if (llist.search(llist.head, 21))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Pratik Agarwal
[ad type=”banner”]

Output:

Yes

Recursive Solution

bool search(head, x)
1) If head is NULL, return false.
2) If head's key is same as x, return true;
2) Else return search(head->next, x)

Java Programming:

// Recursive Java program to search an element
// in linked list


// Node class
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}

// Linked list class
class LinkedList
{
Node head; //Head of list

//Inserts a new node at the front of the list
public void push(int new_data)
{
//Allocate new node and putting data
Node new_node = new Node(new_data);

//Make next of new node as head
new_node.next = head;

//Move the head to point to new Node
head = new_node;
}

// Checks whether the value x is present
// in linked list
public boolean search(Node head, int x)
{
// Base case
if (head == null)
return false;

// If key is present in current node,
// return true
if (head.data == x)
return true;

// Recur for remaining list
return search(head.next, x);
}

// Driver function to test the above functions
public static void main(String args[])
{
// Start with the empty list
LinkedList llist = new LinkedList();

/* Use push() to construct below list
14->21->11->30->10 */
llist.push(10);
llist.push(30);
llist.push(11);
llist.push(21);
llist.push(14);

if (llist.search(llist.head, 21))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Pratik Agarwal
[ad type=”banner”]

Output:

Yes