Timer is Running

Given a linked list, reverse alternate nodes and append at the end
Submissions: 8146   Accuracy:


  Difficulty: Medium   Marks: 4

Given a linked list, you have to perform the following task:

  1. Extract the alternative nodes from starting from second node.
  2. Reverse the extracted list.
  3. Append the extracted list at the end of the original list.

Example 1:

LinkedList = 10->4->9->1->3->5->9->4
Output: 10 9 3 9 4 5 1 4
Explanation: Reversing the alternative
nodes from the given list, and then
appending them to the end of the list
results in a list with the elements as
10 9 3 9 4 5 1 4.

Example 2:

LinkedList = 1->2->3->4->5
Output: 1 3 5 4 2 

Your Task:
The task is to complete the function rearrange() which should rearrange the list as required.

Note: Try to solve the problem without using any extra memory.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

1 <= N <= 50
1 <= Node_value <= 1000

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Tanuj Johal
Author: Jaspreet Singh

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.