Practice questions for Linked List and Recursion | A linked list node ; Prints a linked list in reverse manner ; prints alternate nodes of a Linked List , first from head to end , and then from end to head . ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; put in the data ; link the old list off the new node ; move the head to poto the new node ; Start with the empty list ; Using push ( ) to construct below list 1.2 . 3.4 . 5
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def fun1 ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return NEW_LINE DEDENT fun1 ( head . next ) NEW_LINE print ( head . data , end = " ▁ " ) NEW_LINE DEDENT def fun2 ( start ) : NEW_LINE INDENT if ( start == None ) : NEW_LINE INDENT return NEW_LINE DEDENT print ( start . data , end = " ▁ " ) NEW_LINE if ( start . next != None ) : NEW_LINE INDENT fun2 ( start . next . next ) NEW_LINE DEDENT print ( start . data , end = " ▁ " ) NEW_LINE DEDENT def push ( head , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . next = head NEW_LINE head = new_node NEW_LINE return head NEW_LINE DEDENT head = None NEW_LINE head = Node ( 5 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 3 ) NEW_LINE head = push ( head , 2 ) NEW_LINE head = push ( head , 1 ) NEW_LINE print ( " Output ▁ of ▁ fun1 ( ) ▁ for ▁ list ▁ 1 - > 2 - > 3 - > 4 - > 5" ) NEW_LINE fun1 ( head ) NEW_LINE print ( " Output of fun2 ( ) for list 1 -> 2 -> 3 -> 4 -> 5 " ) NEW_LINE fun2 ( head ) NEW_LINE