- Data Structures
- Linked Lists
- Reverse a doubly linked list
This challenge is part of a tutorial track by MyCodeSchool
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.
Complete the reverse function in the editor below.
reverse has the following parameter(s):
- DoublyLinkedListNode head: a reference to the head of a DoublyLinkedList
- DoublyLinkedListNode: a reference to the head of the reversed list
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.
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.
1 4 1 2 3 4
4 3 2 1
The initial doubly linked list is:
The reversed doubly linked list is: