Copyright. For more information, see the source. Thank you!
Http://blog.csdn.net/walkinginthewind/article/details/7074022
1. The head pointers phead1 and phead2 of two one-way linked lists are given to determine whether the two linked lists are intersecting. Assume that both linked lists do not contain loops.
As follows:
If the two linked lists intersect with one node, all nodes after the intersection node are shared by the two linked lists. That is to say, if the two linked lists intersect, the last node must be the same. Traverse the first linked list first, remember the last node, and then traverse the second linked list to compare the last node with the last node of the first linked list, otherwise, they do not overlap. The time complexity is O (len1 + len2), because only one Extra pointer is required to save the last node address, and the space complexity is O (1 ).
2. The head pointers phead1 and phead2 of two one-way linked lists are given to determine whether the two linked lists are intersecting. If the two linked lists are intersecting, the first node is returned. Assume that both linked lists do not contain loops.
Traverse the first linked list, calculate the length of len1, and save the address of the last node.
Traverse the second linked list, calculate the length of len2, and check whether the last node is the same as the last node of the first linked list. If they are not the same, they do not intersect and end.
The two linked lists start from the first node, assuming that len1 is greater than len2, then the first linked list first traverses the len1-len2 nodes, then the distance between the two linked list current nodes to the first intersection node is equal, compare whether the next node is the same. If the two linked lists are the same, return the same node.
As follows:
3. Give the head pointer phead of a one-way linked list to determine whether there is a ring in the linked list.
As follows:
The linked list contains loops, which are actually self-intersecting. We use two pointers P1 and P2 to traverse the chain table from the beginning. P1 advances one node at a time, and P2 advances two nodes at a time. If there is a ring, P1 and P2 will certainly meet in the ring, if it does not exist, P1 and P2 can reach the last node normally (in fact, they reach null ).
4. The head pointer phead of a one-way linked list is given to determine whether there is a ring in the linked list. If so, find the first node in the ring.
As follows:
The node in the red dotted box is the node to be searched.
First, determine whether a ring exists.
Disconnects a node in the ring (remember to restore the node at the end of the loop), thus forming two single-linked tables with intersection, the first node in the ring is converted to the first node for the intersection of two single-chain tables.
:
Summary: The linked list is the most basic in the data structure and often used in interviews. The topics related to the linked list are also varied. As long as the foundation is solid, you can accumulate some skills to deal with similar problems, the interview is easy to handle.