Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
wont we be required to check the nodes which we are directly skipping .... what if they are merging at the first node itself
Go to the last node in one list say its e, and go through another list check if the next point to the e. Also check if e point to head of another list. If match, then they are merged. This is O(n+m).
not sure how to find merge point efficiently, can do binary search on smaller list if its merged.
Step1:- Find the lenght of both linklist.
let say m&n
Step2:- Now move the pointer of larger linklist by |m-n| node..
Step3:- Be sure pointer of smaller one is on head node & now move the pointer of both linklist.& check for the common address.
another approch to this problem
just traverse both list and put element in two different stack.then start poping element from both stack one by one and check the element,do this until u havent got the unequal element.the answer will be the last match element..
Is this a trick question? In order for two singly linked list to merge/meet at a node, all nodes after the meeting node must be identical as well, which means they must have the same tail node as well (my definition of a tail node is one that has no trailing node). Many linked lists implementations already keep a pointer at the tail, so the access time is O(1) to compare if two lists have merged. If so, traverse backwards until we find the node where they diverge and that's where we stop. In worst case, it's O(n), best case O(1).
Step1:- Find the lenght of both linklist.
- Anonymous May 09, 2012let say m&n
Step2:- Now move the pointer of larger linklist by |m-n| node..
Step3:- Be sure pointer of smaller one is on head node & now move the pointer of both linklist.& check for the common address.