Function Description
Complete the reverse function in the editor below. It should return a reference to the head of your reversed list.
Input 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
4321
Explanation
All we need to do is swap prev and next pointers for all nodes, change prev of the head (or start) and change the head pointer in the end.
class LinkedList {
int data;
Node(int d) {
data = d;
}
}
void reverse() {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
if (temp != null) {
head = temp.prev;
/* UTILITY FUNCTIONS */
/* allocate node */
new_node.prev = null;
new_node.next = head;
if (head != null) {
head.prev = new_node;
head = new_node;
node = node.next;
list.push(2);
list.push(4);
list.push(8);
list.push(10);
list.printList(head);
list.reverse();
System.out.println("");
list.printList(head);
Naive Approach: Sort the given doubly linked list using insertion sort technique.
Time Complexity: O(nk)
Auxiliary Space: O(1)
Efficient Approach: We can sort the list using the MIN HEAP data structure. The approach has been explained in Sort a nearly sorted (or K sorted) array. We only have to be careful while traversing the input doubly linked list and
adjusting the required next and previous links in the final sorted list.
#include <bits/stdc++.h>
using namespace std;
// function to sort a k
sorted doubly linked list
struct Node*
sortAKSortedDLL(struct
Node* head, int k)
{
// if list is empty
if (head == NULL)
return head;
// priority_queue 'pq'
implemeted as min heap
with the
// help of 'compare'
function
priority_queue<Node*,
vector<Node*>, compare>
pq;
// move to the
next node
head = head->next;
}
// place root or
top of 'pq' at the end of
the
// result sorted
list so far having the
first node
// pointed to by
'newHead'
// and adjust the
required links
if (newHead ==
NULL) {
newHead =
pq.top();
newHead->prev
= NULL;
// 'last'
points to the last node
// of the
result sorted list so far
last =
newHead;
}
else {
last->next =
pq.top();
pq.top()->prev
= last;
last =
pq.top();
}
// remove element
from 'pq'
pq.pop();
// if there are
more nodes left in the
input list
if (head != NULL)
{
// push the
node on to 'pq'
pq.push(head);
// move to the
next node
head = head-
>next;
}
}
// making 'next' of
last node point to NULL
last->next = NULL;
// Function to insert a
node at the beginning
// of the Doubly Linked
List
void push(struct Node**
head_ref, int new_data)
{
// allocate node
struct Node* new_node
=
(struct
Node*)malloc(sizeof(struct
Node));
int k = 2;
return 0;
}
Output:
Original Doubly linked list:
3 6 2 12 56 8
Doubly linked list after sorting:
2 3 6 8 12 56
Time Complexity: O(nLogk)
Auxiliary Space: O(k)
Qus 3 : Remove duplicates from a sorted doubly linked list
Level – Hard
Given a sorted doubly linked list containing n nodes. The problem is to remove duplicate nodes from the given list.
Examples:
Algorithm:
removeDuplicates(head_ref, x)
if head_ref == NULL
return
Initialize current = head_ref
while current->next != NULL
if current->data == current->next->data
deleteNode(head_ref, current->next)
else
current = current->next
The algorithm for deleteNode(head_ref, current) (which deletes the node using the pointer to the node) is discussed in this post.
/* If node to be deleted is
head node */
if (*head_ref == del)
*head_ref = del->next;
return 0;
}
Output:
Original Doubly linked list:
4 4 4 4 6 8 8 10 12 12
Doubly linked list after removing duplicates:
4 6 8 10 12
Time Complexity: O(n)