• RSS
  • Facebook
  • Twitter

Knowledge is Power.

  • Who you are ?

    Working on machines without understanding them ? Then you should be here..

  • Where you are ?

    Geographical location should not become a barrier to Share our knowledge.

  • What do you do ?

    Puzzles and Interview question are intended to be discussed here.

    Thursday, February 25, 2010

    Given two sorted Linked Lists, we need to merge them into the third list in sorted order. Complexity – O(n)

    Solution :

       public Node mergeList(Node a, Node b){
    Node result = null;
    if(a==null)
    return b;
    if(b==null)
    return a;

    if(a.data <= b.data){
    result =a;
    result.next = mergeList(a.next,b);
    }
    else
    {
    result =b;
    result.next = mergeList(b.next,a);
    }
    return result;
    }


    0 comments:

    Post a Comment