862 views
2 votes
2 votes
How to trace the below program?

p and q are the starting address of two different linked list

 

 

struct node*Do(struct node*p,struct node*q){
    struct node*ps,*qs;
    if(!p){
        return(q);
    }
    else if(!q){
        return(p);
    }
    else{
        ps=p->link;
        qs=q->link;
        p->link=q;
        q->link=Do(ps,qs);
        return(p);
    }
}

2 Answers

0 votes
0 votes

take 2 different list with same length and blindly apply the code
1) a-b-c-d
2) g-h-i-j

take 2 different list with lengths are not same and blindly apply the code
1) a-b-c-d-e-f
2) g-h-i-j

take 2 different list with lengths are not same and blindly apply the code
1) a-b-c-d
2) g-h-i-j-k-l

 

Answer:: They are merging those lists, where stats with p, take a node from p list and take a node from q list and repeat until one of list completed, after that remaining list is keep it as it is

a-g-b-h-c-i-d-j-e-f is the o/p for the i/p

1) a-b-c-d-e-f
2) g-h-i-j

Related questions

0 votes
0 votes
1 answer
2
Mrityudoot asked Feb 25
144 views
How can we find the highest element in a singly linked list in O(1)? We are free to use any extra space.