Reverse Doubly Linked List in Groups Problem

You are provided with a Doubly Linked List consisting of integers and a positive integer 'K', which represents the size of the group. Your task is to modify the linked list by reversing every group of 'K' nodes in the list.

A doubly linked list is bidirectional, meaning it can be traversed both forward and backward.
Example:
Input:
Linked list: 8 9 10 11 12
K: 3
Output:
10 9 8 12 11
Explanation:
Reverse the first K (3) nodes. The remaining nodes (11 and 12) are less than K, so reverse them as well.

Input:

The first line contains an integer T, indicating the number of test cases.
The first line of each test case provides the elements of the doubly linked list, separated by a space and ending with -1, which is not a list element.
The second line specifies the positive integer 'K'.

Output:

For each test case, output the modified linked list. The elements of the modified list should be single-space separated and end with -1.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 5 * 104
  • 1 <= K <= 105
  • -103 <= data <= 103 (and data != -1)
Note:
You don't have to print anything. Implement the function as directed.
AnswerBot
6d

Reverse groups of K nodes in a doubly linked list

  • Iterate through the linked list in groups of size K

  • Reverse each group of nodes

  • Handle cases where the number of remaining nodes is less than K

Help your peers!
Add answer anonymously...
Zomato 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