VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-alternate-nodes-linked-list-using-recursion/

⇱ Print alternate nodes of a linked list using recursion - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print alternate nodes of a linked list using recursion

Last Updated : 29 Nov, 2022

Given a linked list, print alternate nodes of this linked list.

Examples : 

Input : 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10
Output : 1 -> 3 -> 5 -> 7 -> 9 

Input : 10 -> 9
Output : 10

Recursive Approach : 

  1. Initialize a static variable(say flag) 
  2. If flag is odd print the node 
  3. increase head and flag by 1, and recurse for next nodes.

Implementation:


Output: 
1 3 5 7 9

 

Time complexity: O(N) where N is no of nodes in linked list
Auxiliary space: O(1), If we consider recursive call stack then it would be O(n)

Comment
Article Tags: