-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReverse_a_Doubly_Linked_List.cpp
46 lines (37 loc) · 1.13 KB
/
Reverse_a_Doubly_Linked_List.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Problem Statement:
-----------------
Given a doubly linked list of n elements. The task is to reverse the doubly linked list.
Example 1:
---------
Input:
LinkedList: 3 <--> 4 <--> 5
Output: 5 4 3
Example 2:
---------
Input:
LinkedList: 75 <--> 122 <--> 59 <--> 196
Output: 196 59 122 75
Your Task: Your task is to complete the given function reverseDLL(), which takes head reference as argument and should reverse the elements
so that the tail becomes the new head and all pointers are correctly pointed. You need to return the new head of the reversed list.
The printing and verification is done by the driver code.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
*/
// Link --> https://practice.geeksforgeeks.org/problems/reverse-a-doubly-linked-list/1#
// Code:
Node* reverseDLL(Node * head)
{
if(head == NULL || head->next == NULL)
return head;
Node *temp = head;
Node* temporary = NULL;
while(temp != NULL)
{
temporary = temp->prev;
temp->prev = temp->next;
temp->next = temporary;
temp = temp->prev;
}
return temporary->prev;
}