Search an element in a Linked List ( Iterative and Recursive ) | Node class ; Linked list class Head of list ; Inserts a new node at the front of the list ; Allocate new node and putting data ; Make next of new node as head ; Move the head to povar to new Node ; Checks whether the value x is present in linked list ; Base case ; If key is present in current node , return true ; Recur for remaining list ; * Use push ( ) to construct below list 14 -> 21 -> 11 -> 30 -> 10
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } var head ; function push ( new_data ) { var new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } function search ( head , x ) { if ( head == null ) return false ; if ( head . data == x ) return true ; return search ( head . next , x ) ; } push ( 10 ) ; push ( 30 ) ; push ( 11 ) ; push ( 21 ) ; push ( 14 ) ; if ( search ( head , 21 ) ) document . write ( " " ) ; else document . write ( " " ) ;