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!
Add answer anonymously...
NCR Voyix Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter