Q2 : You are give a node in a linked list and you need to delete it from the list..
Sol: the problem is that we have to delete the node.. but dont know the node before that ... so what we can do is copy the content of the node next to it in the current node .. and simply delete the next node...instead of transfering all teh content iteratively till the end.
Reference :http://www.tekpool.com/node/107
Q3: How to find common node in intersecting linked lists
Sol : There is no need to reverse the link lists. All we need to do is to first calculate len=len1-len2 (len1>len2, as discussed above). Point p1 and p2 to list1 (the longer one) and list2 respectively. Then step only p1 for "len" no. of nodes. After this we are guaranteed that p1 and p2 are equidistant from the common node. So now start stepping p1 and p2 simultaneously until p1==p2. This will the common node. This is a simple O(n) solution.
Sol: the problem is that we have to delete the node.. but dont know the node before that ... so what we can do is copy the content of the node next to it in the current node .. and simply delete the next node...instead of transfering all teh content iteratively till the end.
Reference :http://www.tekpool.com/node/107
Q3: How to find common node in intersecting linked lists
Sol : There is no need to reverse the link lists. All we need to do is to first calculate len=len1-len2 (len1>len2, as discussed above). Point p1 and p2 to list1 (the longer one) and list2 respectively. Then step only p1 for "len" no. of nodes. After this we are guaranteed that p1 and p2 are equidistant from the common node. So now start stepping p1 and p2 simultaneously until p1==p2. This will the common node. This is a simple O(n) solution.
No comments:
Post a Comment