Given a Linked List. The task is to traverse the Linked List from middle to left-right order using recursion.
If the given Linked List is: 2 -> 5 -> 8 -> 3 -> 7 -> 9 -> 12 -> NULL
The Middle to left-right order is : 3, 8, 7, 5, 9, 2, 12
Explanation: Middle of the given linked list is ‘3’ so, we start traversing from middle by printing 3 then left and right of 3, so we print 8, 7 then print left of 8 and right of 7, so we print 5, 9 then print left of 5 and right of 9, so we print 2, 12.
Note: If number of node are even in a Linked List then print left right only. For this linked list( contains even number of nodes ) 2 -> 5 -> 8 -> 7 -> 9 -> 12 -> NULL.
The output should be 8, 7, 5, 9, 2, 12.
Input: 20 -> 15 -> 23 -> 13 -> 19 -> 32 -> 16 -> 41 -> 11 -> NULL
Output: 19, 13, 32, 23, 16, 15, 41, 20, 11.
Input: 12 -> 25 -> 51 -> 16 -> 9 -> 90 -> 7 -> 2 -> NULL
Output: 16, 9, 51, 90, 25, 7, 12, 2.
First, calculate the size of the linked list:
- If size is odd:
-> Then go to the (n+1)/2 -th node using recursion.
- If size is even:
-> Then go to the n/2 -th node using recursion.
- Now print node data and return next node address, do this procedure unless function call stack empty.
Below is the implementation of the above approach:
Created Linked list is: 6-> 4-> 8-> 7-> 9-> 11-> 2 Output : 7 , 8 , 9 , 4 , 11 , 6 , 2
- Delete middle of linked list
- Insert node into the middle of the linked list
- Find the middle of a given linked list in C and Java
- Insert N elements in a Linked List one after other at middle position
- Find middle of singly linked list Recursively
- Find kth node from Middle towards Head of a Linked List
- Make middle node head in a linked list
- Python program to find middle of a linked list using one traversal
- Given a linked list of line segments, remove middle points
- Iterative approach for removing middle points in a linked list of line segements
- Delete a linked list using recursion
- Reverse a Doubly linked list using recursion
- Practice questions for Linked List and Recursion
- Remove duplicates from a sorted linked list using recursion
- Print alternate nodes of a linked list using recursion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.