Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Printing a linked list in a reversed spiral manner

$
0
0

Given a Linked list, the task is to print a singly linked list in a spiral fashion. Starting from the first node then the last node followed by the second node and then the second last node, continuing this pattern until the entire linked list is traversed.for example:

Input:

1 -> 2 -> 3 -> 4 -> 5 -> 6 -> X

Output:

1 -> 6 -> 2 -> 5 -> 3 -> 4 -> X

Input:

1 -> 2 -> 3 -> X

Output:

1 -> 3 -> 2 -> X

I tried making array list and by using two pointers approach to print this solution but it did not work.Can anyone please explain any better approach for the same.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>