Given singly linked list with every node having an additional “arbitrary” pointer that currently points to NULL. Need to make the “arbitrary” pointer point to the next higher value node.
We strongly recommend to minimize your browser and try this yourself first
A Simple Solution is to traverse all nodes one by one, for every node, find the node which has next greater value of the current node and changes the next pointer. Time Complexity of this solution is O(n2).
An Efficient Solution works in O(nLogn) time. The idea is to use Merge Sort for linked list.
1) Traverse input list and copy next pointer to arbit pointer for every node.
2) Do Merge Sort for the linked list formed by arbit pointers.
Below is the implementation of the above idea. All of the merger sort functions are taken from here. The taken functions are modified here so that they work on arbit pointers instead of next pointers.
Result Linked List is: Traversal using Next Pointer 5, 10, 2, 3, Traversal using Arbit Pointer 2, 3, 5, 10,
This article is contributed by Saurabh Bansal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Point arbit pointer to greatest value right side node in a linked list
- Clone a linked list with next and random pointer | Set 1
- Clone a linked list with next and random pointer | Set 2
- Clone a linked list with next and random pointer in O(1) space
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Delete a Node from linked list without head pointer
- Sum of nodes in a linked list which are greater than next node
- Create new linked list from two given linked list with greater element at each node
- How to write C functions that modify head pointer of a Linked List?
- Delete multiple occurrences of key in Linked list using double pointer
- Correct the Random Pointer in Doubly Linked List
- Insertion in a sorted circular linked list when a random pointer is given
- Next greater element in the Linked List
- C Program to reverse each node value in Singly Linked List
- Swap Kth node from beginning with Kth node from end in a Linked List
- Swap Kth node from beginning with Kth node from end in a Doubly Linked List
- Insert a node in Linked List before a given node
- Bitonic point in the given linked list
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1