# Reverse a doubly linked list HackerRank Solution Reverse a doubly linked list HackerRank Solution

In this Reverse a doubly linked list HackerRank solution, Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list.

Note: The head node might be NULL to indicate that the list is empty.

Function Description

Complete the reverse function in the editor below.

reverse has the following parameter(s):

Returns

Input Format

The first line contains an integer , the number of test cases.

Each test case is of the following format:

• The first line contains an integer , the number of elements in the linked list.
• The next  lines contain an integer each denoting an element of the linked list.

Constraints

Output Format

Return a reference to the head of your reversed list. The provided code will print the reverse array as a one line of space-separated integers for each test case.

Sample Input

1
4
1
2
3
4


Sample Output

4 3 2 1


Explanation

The initial doubly linked list is:

The reversed doubly linked list is:

Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list.

Note: The head node might be NULL to indicate that the list is empty.

Function Description

Complete the reverse function in the editor below.

reverse has the following parameter(s):

Returns

Input Format

The first line contains an integer , the number of test cases.

Each test case is of the following format:

• The first line contains an integer , the number of elements in the linked list.
• The next  lines contain an integer each denoting an element of the linked list.

Constraints

Output Format

Return a reference to the head of your reversed list. The provided code will print the reverse array as a one line of space-separated integers for each test case.

Sample Input

1
4
1
2
3
4


Sample Output

4 3 2 1


## Reverse a doubly linked list HackerRank solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

### Problem Solution in Python

def reverse(llist):
if llist == None or llist.next == None:
return llist

while llist != None:
nxt = llist.next

llist.next = llist.prev
llist.prev = nxt

if nxt is None: break

llist = nxt
return llist

### Problem Solution in Java

 public static DoublyLinkedListNode reverse(DoublyLinkedListNode llist) {
while(current!=null){

next = current.next;
current.next = prev;
current.prev = next;
prev = current;
current = next;
}
current = prev;

return current;

}
Solve original Problem on HackerRank here. Checkout more HackerRank Problems