1.

Given a singly linked list with an additional "arbitrary" pointer at each node that currently points to NULL. What algorithm will you implement to make the "arbitrary" pointer point to the next node with a greater value?

Answer»

A simple SOLUTION is to go through all nodes one by one, finding the node with the next bigger VALUE than the current node and changing the next pointer for each node. This solution has an O time complexity (n^2).

An Efficient Solution takes O(nLogn) time to complete. The APPROACH is to use MERGE Sort for linked lists.

  • Traverse input list and for each node, copy the next pointer to arbit pointer.
  • Sort the linked list formed by arbit pointers USING Merge Sort.

Here, all of the merge sort methods are altered to work with arbit pointers rather than the next pointers.



Discussion

No Comment Found