X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Rotate doubly Linked List
Easy Accuracy: 55.04% Submissions: 18764 Points: 2

Given a doubly linked list, rotate the linked list counter-clockwise by P nodes. Here P is a given positive integer and is smaller than the count of nodes(N) in a linked list.

Example 1:

Input: 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6 , P = 2
Output: 3 <-> 4 <-> 5 <-> 6 <-> 1 <-> 2
Explanation: Doubly linked list after rotating
2 nodes is: 3 4 5 6 1 2.

Example 2:

Input: 3 <-> 4 <-> 5 <-> 1 ,P = 3
Output: 1<-> 3 <-> 4 <-> 5

 

Your Task:
You don't need to read input or print anything. Your task is to complete the function rotateDLL() which takes head node of the linked list and an integer P as input parameters and returns the head node after rotating the linked list by P nodes.


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
2 <= N <= 100
1 <= P <  N

 

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Rotate doubly Linked List

Output Window