Correct me if am wrong.. i think we can use Stack as follows
node * minFun(node *head)
{
stack<node *> st;
return fun(head,st);
}
node * fun(node *ptr,stack<node *> &st)
{
if(ptr)
{
node *x=fun(ptr->next,st);
while(!st.empty() && (ptr->data)>(st.top()->data))
st.pop();
if(!st.empty())
ptr->high=st.top();
st.push(ptr);
return (x?((ptr->data)<(x->data)?ptr:x):ptr);
}
return NULL;
}
On Sun, Mar 25, 2012 at 12:20 AM, atul anand <[email protected]>wrote:
> after push(&s,next) move head also
> head=head->next;
>
>
> On Sun, Mar 25, 2012 at 12:10 AM, atul anand <[email protected]>wrote:
>
>> @all : i am getting this right , i guess given a linked list ...you need
>> to point to next larger element.
>> so if input linked list is 7 3 5 1 5 9
>> then nextLarger of each node will point as follows:-
>>
>> 3->5
>> 1->5
>> 5->9
>> 7->9
>> 9->NULL;
>>
>> i have no idea why the linked list is modified using merge sort...
>> anywayzz if the expected output is similar to one i have mentioned above
>> there this woud work using stack .. complexity O(n) n=number of nodes.
>>
>> findLarger(node *head)
>> {
>> s1 s;
>> node *ele,*next;
>>
>> s.top=NULL;
>> push(&s,head);
>> head=head->next;
>> while(head!=NULL)
>> {
>> next->data=head->data;
>> if(isEmpty(&s))
>> {
>> ele=pop(&s);
>> while(ele->data < next->data)
>> {
>> ele->nextLarger=next;
>> if(isEmpty(&s)==0)
>> {
>> break;
>> }
>> ele=pop(&s);
>> }
>> if(ele->data > next->data)
>> {
>> push(&s,ele);
>> }
>>
>> }
>>
>> push(&s,next);
>> }
>>
>> On Sat, Mar 24, 2012 at 12:50 AM, Atul Singh <[email protected]>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 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.
>>>
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> 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.
>
--
*Dheeraj Sharma*
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
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.