Single-strand surface test Set

Source: Internet
Author: User
1. Find the penultimate element of a single linked list (only allow traversal of the linked list)
Use the pointer to catch up with the method. Define two pointers fast and slow,fast take the K step first, then fast and slow continue to go. When fast reaches the end of the list, the slow points to the countdown K. Note that you should consider that the list length should be greater than K. 2. Find the middle element of a single linked list (only allow traversal of the linked list)
Use the pointer to catch up with the method. Fast every step of the way, slow one step at a time. When fast is at the end of the list, slow points to the middle element of the list.
3, to determine whether the single-linked list has a ring.
Method One: Use the pointer catching method. The slow pointer goes one step at a time, and the fast pointer goes two steps at a time. If there is a ring, the two meet; fast encounters a null exit if no ring exists.
Method Two: Use P, q Two pointers, p always go forward, but Q every time to start from the beginning. 4, how to know the length of the ring.
Record the collision point (or any one of the nodes in the ring can), let slow start from the collision point, around the ring walk, again to the location of the collision point, the number of nodes traversed is the length of the ring S.           5, how to find the entrance of the ring. Starting from the collision point, the head pointer, the meeting point is the connection point. 6. Determine if the two linked lists (no loops) intersect.
Method One: Using the method of violence, traverse two linked lists, and compare them in the process of traversal to see if the nodes are the same.
Method Two: Once the linked list intersect, the intersection node must have the same memory address, so the memory address to establish a hash table, so by judging whether there are two linked lists of the same memory address node to determine whether the two linked list intersect. This is done by traversing the first linked list and using the address to set up a hash table, traversing the second list to see if the address hash is the same as the node address value in the first table to determine if the two linked lists intersect. Time Complexity O (length (A) + length (B))
Method Three: Problem transformation method. First, the first list is traversed to its tail, and then the trailing next pointer pointing to null points to the second list. In this way, two linked lists are synthesized into a linked list, and the problem is changed to determine whether the new list has loops.
Method Four: Once the two linked lists intersect, then the two linked lists from the intersection node to the tail node must be the same node. So, if they intersect, then their last node must be the same, so they traverse to the end of the two lists, and then determine if they are the same. 7. How to know if two single-linked lists (possibly with loops) intersect
Idea: According to the two linked list whether there is a ring to deal with, if the intersection of this ring belongs to two linked list of total
(1) If none of the two linked lists have a ring.
(2) One with ring, one without ring. Definitely not intersect.
(3) Two have a ring.      ① The ring entry of a to determine whether it is on the B-linked list, and if so, intersects. ② on a linked list, using the method of the pointer chase, find two pointer collision points, and then determine whether the collision point on the B-linked list. If at, then intersect. 8. Find the first public node of the two linked list
Method One: The simplest method is to sequentially access one of the linked lists, each time a node is accessed, the other linked list is traversed until an equal node location is found, if the linked list length is m,n, the time complexity is O (MN).
Method Two: (two cases) ① intersection point, outside the ring. If the two linked list has a common node, then all the nodes after the common node are all two linked lists, so the length must also be equal, if the total length of the two linked list is equal, then we traverse the two linked list, we must reach the first public node at the same time. However, the length of the list is not necessarily the same, so calculate the length of the two list of the difference N, and then let the long list first move n, the short list and then start to traverse backward, so that they must simultaneously reach the first public node, We only need to compare the nodes of the two linked lists when moving backwards to get the first public node. The complexity of Time is O (m+n).
② intersect the point within the ring. When the intersection is in the ring, the intersection point at this point can be the ring entry in the A-linked list, or it can be a ring entry point in the B-linked list. This is because if you see B as a complete list, and a points to the B-linked list, the intersection point is the ring entry for a.           Conversely, the intersection is the ring entry point of List B. Idea: According to the above analysis, you can directly find the ring entry point of a or B of the ring entry point can be.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.