Check if two linked lists merge. If so, where?

后端 未结 26 1385
陌清茗
陌清茗 2020-12-07 06:43

This question may be old, but I couldn\'t think of an answer.

Say, there are two lists of different lengths, merging at a point; how do we know wher

相关标签:
26条回答
  • 2020-12-07 07:11

    Here's a solution, computationally quick (iterates each list once) but uses a lot of memory:

    for each item in list a
      push pointer to item onto stack_a
    
    for each item in list b
      push pointer to item onto stack_b
    
    while (stack_a top == stack_b top) // where top is the item to be popped next
      pop stack_a
      pop stack_b
    
    // values at the top of each stack are the items prior to the merged item
    
    0 讨论(0)
  • 2020-12-07 07:11

    We can efficiently solve it by introducing "isVisited" field. Traverse first list and set "isVisited" value to "true" for all nodes till end. Now start from second and find first node where flag is true and Boom ,its your merging point.

    0 讨论(0)
  • 2020-12-07 07:12

    Solution using javascript

    var getIntersectionNode = function(headA, headB) {
        
        if(headA == null || headB == null) return null;
        
        let countA = listCount(headA);
        let countB = listCount(headB);
        
        let diff = 0;
        if(countA > countB) {
    
            diff = countA - countB;
            for(let i = 0; i < diff; i++) {
                headA = headA.next;
            }
        } else if(countA < countB) {
            diff = countB - countA;
            for(let i = 0; i < diff; i++) {
                headB = headB.next;
            }
        }
    
        return getIntersectValue(headA, headB);
    };
    
    function listCount(head) {
        let count = 0;
        while(head) {
            count++;
            head = head.next;
        }
        return count;
    }
    
    function getIntersectValue(headA, headB) {
        while(headA && headB) {
            if(headA === headB) {
                return headA;
            }
            headA = headA.next;
            headB = headB.next;
        }
        return null;
    }
    
    0 讨论(0)
  • 2020-12-07 07:13

    Steps in Java:

    1. Create a map.
    2. Start traversing in the both branches of list and Put all traversed nodes of list into the Map using some unique thing related to Nodes(say node Id) as Key and put Values as 1 in the starting for all.
    3. When ever first duplicate key comes, increment the value for that Key (let say now its value became 2 which is > 1.
    4. Get the Key where the value is greater than 1 and that should be the node where two lists are merging.
    0 讨论(0)
  • 2020-12-07 07:14

    There can be a simple solution but will require an auxilary space. The idea is to traverse a list and store each address in a hash map, now traverse the other list and match if the address lies in the hash map or not. Each list is traversed only once. There's no modification to any list. Length is still unknown. Auxiliary space used: O(n) where 'n' is the length of first list traversed.

    0 讨论(0)
  • 2020-12-07 07:15

    this solution iterates each list only once...no modification of list required too..though you may complain about space..
    1) Basically you iterate in list1 and store the address of each node in an array(which stores unsigned int value)
    2) Then you iterate list2, and for each node's address ---> you search through the array that you find a match or not...if you do then this is the merging node

    //pseudocode
    //for the first list
    p1=list1;
    unsigned int addr[];//to store addresses
    i=0;
    while(p1!=null){
      addr[i]=&p1;
      p1=p1->next;
    }
    int len=sizeof(addr)/sizeof(int);//calculates length of array addr
    //for the second list
    p2=list2;
    while(p2!=null){
      if(search(addr[],len,&p2)==1)//match found
      {
        //this is the merging node
        return (p2);
      }
      p2=p2->next;
    }
    
    int search(addr,len,p2){
      i=0;  
      while(i<len){
        if(addr[i]==p2)
          return 1;
        i++;
      }
     return 0;
    } 
    

    Hope it is a valid solution...

    0 讨论(0)
提交回复
热议问题