using stack, the problem can be solved in O(n) time. here is the algo :- 1- push first node in stack. mark next node as current 2 - start from current element and check if the node on top of stack is smaller than current , then pop the node and make its next_largest pointer set to current. Do this until the stack is empty or the node on top of stack becomex greater than current. 3- If the node on top of stack is greater than current, push the current node on top of the stack. Move current to current->next 4 - Do this iteration for all nodes until current becomes NULL 5 - if stack is empty,we are done else pop each element of stack and make its next_largest point to NULL
On Saturday, 24 March 2012 00:50:43 UTC+5:30, ATul SIngh wrote: > > Given a linked list with each node having two pointers : one pointing to > next node & other to null; > how will u point the second pointer to next larger no. and return the > pointer to smallest node > > > > -- > ATul Singh | Final Year | Computer Science & Engineering | NIT Jalandhar > | 9530739855 | > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/RJYgDkGw6NsJ. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
