Reverse a Doubly Linked List

Given a doubly linked list of positive integers with size 'N', reverse the list and return the head of the modified list.

Explanation:

A doubly linked list allows traversal in both directions, forward and backward.

Input:

The input begins with an integer 'T', representing the number of test cases.
Each test case comprises elements of the doubly linked list, separated by a space and terminated by -1, indicating the end of the list.

Output:

For each test case, output the elements of the reversed list, separated by a space and followed by -1.
Provide the output of each test case in a separate line.

Example:

Input:
2
1 2 3 -1
4 5 6 7 -1
Output:
3 2 1 -1
7 6 5 4 -1

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 10^4
  • 0 <= data <= 10^3
AnswerBot
1y

The task is to reverse a given doubly linked list and return the head of the modified list.

  • Create three pointers: prev, current, and next.

  • Iterate through the linked list and update the next and prev p...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Intern Interview Questions Asked at NCR Corporation

Q. Longest Palindromic Substring Problem Statement You are provided with a string S...read more
Q. Count Distinct Ways to Reach the Nth Stairs Consider a scenario where you have b...read more
Q. Can you describe your projects and the tech stack you used in them?
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