AdSense

Thursday, December 11, 2014

Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists: 
A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗            
B:     b1 → b2 → b3
begin to intersect at node c1.

Notes:
  • If the two linked lists have no intersection at all, return null.
  • The linked lists must retain their original structure after the function returns. 
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.

A fresh new problem. Somehow I am very in to linked list. I think I am just having fun play around with pointers. 

Back to the problem. I found this method through GeeksforGeeks, and implemented it using Java. Consider I am such a fake one, I have no guilty borrowing it. 

1. Determine the size of both linked lists.  
2. Get the difference between two lists diff = abs(sizeA - sizeB). 
3. Traverse the list with the larger size until node diff. 
4. Traverse both lists together, if both the value and pointer of the nodes are the same in two lists, we can conclude they intersect. Otherwise, if nothing pops up till the end of both lists, return null. 

Mission accomplished. :)

public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
      }
  }
 
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null)
            return null;
        int sizeA = getSize(headA);
        int sizeB = getSize(headB);
        int diff = Math.abs(sizeA - sizeB);
        int index = 0;
        if (sizeA > sizeB)
        {
            while (headA != null && index < diff)
            {
                headA = headA.next;
                index++;
            }
        }
        else
        {
            while(headB != null && index < diff)
            {
                headB = headB.next;
                index++;
            }
        }
        while (headA != null && headB != null)
        {
            if (headA.val == headB.val && headA.next == headB.next)
                return headA;
            headA = headA.next;
            headB = headB.next;
        }
        return null;
        
        
    }
    private int getSize(ListNode head)
    {
        int size = 0;
        while (head != null)
        {
            size++;
            head = head.next;
        }
        return size;
    }
}

1 comment:


  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete