Reverse Alternate Nodes in a Singly Linked List

Given a singly linked list of integers, you need to reverse alternate nodes and append them to the end of the list.

Example:

Input:
1->2->3->4
Output:
1->3->4->2
Explanation:

Assuming 0-based indexing, odd-indexed nodes are considered alternate. In this case, nodes with values 2 and 4 are alternates. Separate them: 1->3 (without alternates) and 2->4 (alternates). Reverse alternates: 4->2. Append reversed alternates to the end: 1->3->4->2.

Input:

The first and only line contains elements of the singly linked list separated by spaces, terminated by -1. (-1 is not a list element.)

Output:

For each test case, output is a space-separated list of integers representing the resultant linked list, ending with -1.

Constraints:

  • 0 <= N <= 10^6
  • -10^9 <= DATA <= 10^9 and DATA != -1

Where N is the number of nodes in the linked list.

Time Limit: 1 second.

Note:

You are not required to print anything; just implement the function as specified.

AnswerBot
4mo

Reverse alternate nodes in a singly linked list and append them to the end of the list.

  • Traverse the linked list and reverse alternate nodes while keeping track of odd and even indices.

  • Append the rever...read more

Help your peers!
Select
Add answer anonymously...

TCS iON Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Fitness Test in Indian Navy Problem Statement The selection process in the India...read more
A Software Developer Intern was asked Q. Nth Element Of Modified Fibonacci Series Given two integers X and Y as the first...read more
A Software Developer Intern was asked Q. Reverse Alternate Nodes in a Singly Linked List Given a singly linked list of in...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Fitness Test in Indian Navy Problem Statement The selection process in the India...read more
A Software Developer Intern was asked Q2. Nth Element Of Modified Fibonacci Series Given two integers X and Y as the first...read more
A Software Developer Intern was asked Q3. Reverse Alternate Nodes in a Singly Linked List Given a singly linked list of in...read more
TCS iON Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits