Open In App

Practice questions for Linked List and Recursion

Last Updated : 14 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Assume the structure of a Linked List node is as follows. 

C++




structNode
{
  intdata;
  structNode *next;
};
 
// This code is contributed by SHUBHAMSINGH10

C




structNode
{
  intdata;
  structNode *next;
};

Java




staticclassNode
{
    intdata;
    Node next;
};
 
// This code is contributed by shubhamsingh10

Python3




classNode:
    def__init__(self, data):
        self.data =data
        self.next=None

C#




publicclassNode
    publicintdata;
    publicNode next;
};
 
// This code is contributed by pratham_76

Javascript




<script>
 
class Node
{
    constructor(item)
    {
        this.data = item;
        this.next = null;
    }
}
  
// This code contributed by shubhamsingh10
</script>

Explain the functionality of the following C functions.

1. What does the following function do for a given Linked List?

C++14




voidfun1(structNode* head)
{
  if(head == NULL)
      return;
 
  fun1(head->next);
  cout << head->data << " ";
}
 
// This code is contributed by shubhamsingh10

C




voidfun1(structNode* head)
{
  if(head == NULL)
    return;
  
  fun1(head->next);
  printf("%d  ", head->data);
}

Java




staticvoidfun1(Node head)
{
    if(head == null)
    {
        return;
    }
 
    fun1(head.next);
    System.out.print(head.data + " ");
}
 
// This code is contributed by shubhamsingh10

Python




deffun1(head):
    if(head ==None):
        return
    fun1(head.next)
    print(head.data, end =" ")
 
# This code is contributed by shubhamsingh10

C#




staticvoidfun1(Node head)
{
    if(head == null)
    {
        return;
    }
 
    fun1(head.next);
    Console.Write(head.data + " ");
}
 
// This code is contributed by shubhamsingh10

Javascript




<script>
 
// Javascript Implementation
functionfun1( head)
{
  if(head == null)
      return;
 
  fun1(head.next);
  document.write(head.data);
}
 
// This code is contributed by shubhamsingh10
</script>

fun1() prints the given Linked List in the reverse way. For Linked List 1->2->3->4->5, fun1() prints 5->4->3->2->1.

2. What does the following function do for a given Linked List? 

C++




voidfun2(structNode* head)
{
    if(head == NULL)
        return;
    cout << head->data << " ";
     
    if(head->next != NULL )
        fun2(head->next->next);
    cout << head->data << " ";
}
 
// This code is contributed by shubhamsingh10

C




voidfun2(structNode* head)
{
  if(head == NULL)
    return;
  printf("%d  ", head->data);
 
  if(head->next != NULL )
    fun2(head->next->next);
  printf("%d  ", head->data);  
}

Java




staticvoidfun2(Node head)
{
    if(head == null)
    {
        return;
    }
    System.out.print(head.data + " ");
 
    if(head.next != null)
    {
        fun2(head.next.next);
    }
    System.out.print(head.data + " ");
}
 
// This code is contributed by shubhamsingh10

Python3




deffun2(head):
      
    if(head ==None):
        return
    print(head.data, end =" ")
      
    if(head.next!=None):
        fun2(head.next.next)
    print(head.data, end =" ")
 
    # This code is contributed by divyesh072019

C#




staticvoidfun2(Node head)
{
    if(head == null)
    {
        return;
    }
    Console.Write(head.data + " ");
 
    if(head.next != null)
    {
        fun2(head.next.next);
    }
    Console.Write(head.data + " ");
}
 
// This code is contributed by divyeshrabadiya07

Javascript




<script>
 
// Javascript Implementation
functionfun2( head)
{
  if(head == null)
      return;
  document.write(head.data);
   
  if(head.next != null)
      fun2(head.next.next);
  document.write(head.data);
}
 
// This code is contributed by shubhamsingh10
 
</script>

fun2() prints alternate nodes of the given Linked List, first from head to end, and then from end to head. If Linked List has even number of nodes, then fun2() skips the last node. For Linked List 1->2->3->4->5, fun2() prints 1 3 5 5 3 1. For Linked List 1->2->3->4->5->6, fun2() prints 1 3 5 5 3 1.

Below is a complete running program to test the above functions.

C++




#include <bits/stdc++.h>
usingnamespacestd;
 
/* A linked list node */
classNode
{
    public:
    intdata;
    Node *next;
};
 
 
/* Prints a linked list in reverse manner */
voidfun1(Node* head)
{
    if(head == NULL)
        return;
     
    fun1(head->next);
    cout << head->data << " ";
}
 
/* prints alternate nodes of a Linked List, first
from head to end, and then from end to head. */
voidfun2(Node* start)
{
    if(start == NULL)
        return;
    cout<<start->data<<" ";
     
    if(start->next != NULL )
        fun2(start->next->next);
    cout << start->data << " ";
}
 
/* UTILITY FUNCTIONS TO TEST fun1() and fun2() */
/* 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. */
voidpush(Node** head_ref, intnew_data)
{
    /* allocate node */
    Node* new_node = newNode();
     
    /* put in the data */
    new_node->data = new_data;
     
    /* link the old list of the new node */
    new_node->next = (*head_ref);
     
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
 
/* Driver code */
intmain()
{
    /* Start with the empty list */
    Node* head = NULL;
     
    /* Using push() to construct below list
        1->2->3->4->5 */
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
     
    cout<<"Output of fun1() for list 1->2->3->4->5 \n";
    fun1(head);
     
    cout<<"\nOutput of fun2() for list 1->2->3->4->5 \n";
    fun2(head);
 
    return0;
}
 
// This code is contributed by rathbhupendra

C




#include<stdio.h>
#include<stdlib.h>
 
/* A linked list node */
structNode
{
  intdata;
  structNode *next;
};
 
 
/* Prints a linked list in reverse manner */
voidfun1(structNode* head)
{
  if(head == NULL)
    return;
 
  fun1(head->next);
  printf("%d  ", head->data);
}
 
/* prints alternate nodes of a Linked List, first
  from head to end, and then from end to head. */
voidfun2(structNode* start)
{
  if(start == NULL)
    return;
  printf("%d  ", start->data);
 
  if(start->next != NULL )
    fun2(start->next->next);
  printf("%d  ", start->data);
}
 
/* UTILITY FUNCTIONS TO TEST fun1() and fun2() */
/* 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. */
voidpush(structNode** head_ref, intnew_data)
{
  /* allocate node */
  structNode* new_node =
          (structNode*) malloc(sizeof(structNode));
  
  /* put in the data  */
  new_node->data  = new_data;
  
  /* link the old list of the new node */
  new_node->next = (*head_ref);
  
  /* move the head to point to the new node */
  (*head_ref)    = new_node;
}
  
/* Driver program to test above functions */
intmain()
{
  /* Start with the empty list */
  structNode* head = NULL;
 
  /* Using push() to construct below list
    1->2->3->4->5  */
  push(&head, 5);
  push(&head, 4);
  push(&head, 3);
  push(&head, 2);
  push(&head, 1);  
  
  printf("Output of fun1() for list 1->2->3->4->5 \n");
  fun1(head);
 
  printf("\nOutput of fun2() for list 1->2->3->4->5 \n");
  fun2(head);
         
  getchar();
  return0;
}

Java




// Java code implementation for above approach
classGFG
{
 
    /* A linked list node */
    staticclassNode
    {
        intdata;
        Node next;
    };
 
    /* Prints a linked list in reverse manner */
    staticvoidfun1(Node head)
    {
        if(head == null)
        {
            return;
        }
 
        fun1(head.next);
        System.out.print(head.data + " ");
    }
 
    /* prints alternate nodes of a Linked List, first
    from head to end, and then from end to head. */
    staticvoidfun2(Node start)
    {
        if(start == null)
        {
            return;
        }
        System.out.print(start.data + " ");
 
        if(start.next != null)
        {
            fun2(start.next.next);
        }
        System.out.print(start.data + " ");
    }
 
    /* UTILITY FUNCTIONS TO TEST fun1() and fun2() */
    /* 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. */
    staticNode push(Node head_ref, intnew_data)
    {
        /* allocate node */
        Node new_node = newNode();
 
        /* put in the data */
        new_node.data = new_data;
 
        /* link the old list of the new node */
        new_node.next = (head_ref);
 
        /* move the head to point to the new node */
        (head_ref) = new_node;
        returnhead_ref;
    }
 
    /* Driver code */
    publicstaticvoidmain(String[] args)
    {
        /* Start with the empty list */
        Node head = null;
 
        /* Using push() to construct below list
        1->2->3->4->5 */
        head = push(head, 5);
        head = push(head, 4);
        head = push(head, 3);
        head = push(head, 2);
        head = push(head, 1);
 
        System.out.print("Output of fun1() for "+
                         "list 1->2->3->4->5 \n");
        fun1(head);
 
        System.out.print("\nOutput of fun2() for "+
                           "list 1->2->3->4->5 \n");
        fun2(head);
    }
}
 
// This code is contributed by Rajput-Ji

Python3




''' A linked list node '''
classNode:
    def__init__(self, data):
        self.data =data
        self.next=None
 
''' Prints a linked list in reverse manner '''
deffun1(head):
    if(head ==None):
        return
    fun1(head.next)
    print(head.data, end =" ")
 
''' prints alternate nodes of a Linked List, first
from head to end, and then from end to head. '''
deffun2(start):
     
    if(start ==None):
        return
    print(start.data, end =" ")
     
    if(start.next!=None):
        fun2(start.next.next)
    print(start.data, end =" ")
 
 
''' UTILITY FUNCTIONS TO TEST fun1() and fun2() '''
''' 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. '''
defpush( head, new_data):
     
    ''' put in the data '''
    new_node =Node(new_data)
     
    ''' link the old list of the new node '''
    new_node.next=head
     
    ''' move the head to point to the new node '''
    head =new_node
    returnhead
 
''' Driver code '''
''' Start with the empty list '''
head =None
 
''' Using push() to construct below list
    1.2.3.4.5 '''
head =Node(5)
head =push(head, 4)
head =push(head, 3)
head =push(head, 2)
head =push(head, 1)
 
print("Output of fun1() for list 1->2->3->4->5")
fun1(head)
 
print("\nOutput of fun2() for list 1->2->3->4->5")
fun2(head)
 
# This code is contributed by SHUBHAMSINGH10

C#




// C# code implementation for above approach
usingSystem;
 
classGFG
{
 
    /* A linked list node */
    publicclassNode
    {
        publicintdata;
        publicNode next;
    };
 
    /* Prints a linked list in reverse manner */
    staticvoidfun1(Node head)
    {
        if(head == null)
        {
            return;
        }
 
        fun1(head.next);
        Console.Write(head.data + " ");
    }
 
    /* prints alternate nodes of a Linked List, first
    from head to end, and then from end to head. */
    staticvoidfun2(Node start)
    {
        if(start == null)
        {
            return;
        }
        Console.Write(start.data + " ");
 
        if(start.next != null)
        {
            fun2(start.next.next);
        }
        Console.Write(start.data + " ");
    }
 
    /* UTILITY FUNCTIONS TO TEST fun1() and fun2() */
    /* 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. */
    staticNode Push(Node head_ref, intnew_data)
    {
        /* allocate node */
        Node new_node = newNode();
 
        /* put in the data */
        new_node.data = new_data;
 
        /* link the old list of the new node */
        new_node.next = (head_ref);
 
        /* move the head to point to the new node */
        (head_ref) = new_node;
        returnhead_ref;
    }
 
    /* Driver code */
    publicstaticvoidMain(String[] args)
    {
        /* Start with the empty list */
        Node head = null;
 
        /* Using.Push() to construct below list
        1->2->3->4->5 */
        head = Push(head, 5);
        head = Push(head, 4);
        head = Push(head, 3);
        head = Push(head, 2);
        head = Push(head, 1);
 
        Console.Write("Output of fun1() for "+
                        "list 1->2->3->4->5 \n");
        fun1(head);
 
        Console.Write("\nOutput of fun2() for "+
                        "list 1->2->3->4->5 \n");
        fun2(head);
    }
}
 
// This code is contributed by Rajput-Ji

Javascript




<script>
    // Javascript code implementation for above approach
     
    /* A linked list node */
    class Node
    {
        constructor(data) {
           this.next = null;
           this.data = data;
        }
    }
     
    /* Prints a linked list in reverse manner */
    functionfun1(head)
    {
        if(head == null)
        {
            return;
        }
  
        fun1(head.next);
        document.write(head.data + " ");
    }
  
    /* prints alternate nodes of a Linked List, first
    from head to end, and then from end to head. */
    functionfun2(start)
    {
        if(start == null)
        {
            return;
        }
        document.write(start.data + " ");
  
        if(start.next != null)
        {
            fun2(start.next.next);
        }
        document.write(start.data + " ");
    }
  
    /* UTILITY FUNCTIONS TO TEST fun1() and fun2() */
    /* 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. */
    functionPush(head_ref, new_data)
    {
        /* allocate node */
        /* put in the data */
        let new_node = newNode(new_data);
  
        /* link the old list of the new node */
        new_node.next = (head_ref);
  
        /* move the head to point to the new node */
        (head_ref) = new_node;
        returnhead_ref;
    }
     
    /* Start with the empty list */
    let head = null;
 
    /* Using.Push() to construct below list
          1->2->3->4->5 */
    head = Push(head, 5);
    head = Push(head, 4);
    head = Push(head, 3);
    head = Push(head, 2);
    head = Push(head, 1);
 
    document.write("Output of fun1() for "+
                  "list 1->2->3->4->5 "+ "</br>");
    fun1(head);
     
    document.write("</br>");
    document.write("Output of fun2() for "+
                  "list 1->2->3->4->5 "+ "</br>");
    fun2(head);
    
   // This code is contributed by mukesh07.
</script>

Output: 

 Output of fun1() for list 1->2->3->4->5 5 4 3 2 1 Output of fun2() for list 1->2->3->4->5 1 3 5 5 3 1

Time complexity: O(n)

Auxiliary Space: O(1)

Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.
 



Next Article
Practice Tags :

Similar Reads

close