Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Given two linked lists, insert nodes of second list into first list at alternate positions of first list.

eg. Given two lists: 1->5->7->3->9 and 6->10->2->4, the first list should become 1->5->6->10->7->3->2->4->9 and second list should become empty.

If the first list is 1->2->3 and second list is 4->5->6->7, then first list should become 1->2->4->5->3->6>7

here is what I have :
Java
public SLL joinLists(SLL in) {

    //SLL<e> res = new SLL<e>();
    SLLNode<e> tmp1=this.getFirst(); 
    SLLNode<e> tmp2= in.getFirst();

    while(tmp1.succ!=null && tmp2.succ!=null){
        tmp1 = tmp1.succ;
        this.insertAfter(tmp2.element,tmp1);
        tmp1=tmp1.succ;
        if(tmp2.succ!=null){
            tmp2 = tmp2.succ;
            this.insertAfter(tmp2.element,tmp1);
            tmp1=tmp1.succ;        
        }
        if(tmp1.succ!=null)
            tmp1=tmp1.succ;        
    }

    while(tmp2!=null){
        this.insertAfter(tmp2.element,tmp1);
            tmp1=tmp1.succ;
    }
    return this;
}
Posted
Updated 15-Oct-14 11:07am
v3
Comments
Sergey Alexandrovich Kryukov 15-Oct-14 14:23pm    
Not a question. Isn't it an order to do your homework? (Debugging of your code is still doing your work for you, unless you describe your problem you faced.)
—SA
joshrduncan2012 15-Oct-14 17:25pm    
So what is your question? You've presented code to us as a result of a problem, but no question as a result of it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900