Linked List Classic Exercises

Source: Internet
Author: User

summed up the beauty of programming above on the list of problems, there is not the right place, welcome to shoot bricks, and other programming beauty to read, back to brush other problems encountered linked list and then add ~The catalogue is as follows (click to expand the above directory to the topic of interest):
/************************************************************************//* Linked List Classic exercises 1. Removing nodes from the Headless List 2. List Reversal 3. Determine if two linked lists intersect 4. How to check if the chain list exists ring 5. The length of the ring in the ring chain is 6. Find the ring connection point position of the ring-linked list 7. If the linked list may have rings, determine if the two linked lists Intersect, and The first node to intersect *//************************************************************************/


1. Removing nodes from the Headless listTitle Description: Given a single linked list without a head pointer, a pointer to a node that is neither the first node nor the last node, requires that the point to which the pointer points is deletedidea: This problem comes from the beauty of programming, on page 226. At first, the first time to see this problem, feel seems unable to delete, read the answer to realize that the idea is to delete the node behind the pointer, the deletion of the data field of the node in advance to overwrite the data field of the pointer node, it is clever, the code is as follows:
1. Remove the node void Deletenode (Pnode p) {if (NULL = = p) from the Headless list and/or ensure the legality of P {return;} Pnode next = p->pnext;//to remove the node's next node if (next! = NULL)//Ensure that the node being deleted is not the last node {p->data = next->data;//data field content overwrite p-> Pnext = next->pnext;//Delete the node that points to delete next;next = NULL;}}


2. List reversalTitle Description: Given a single-linked list of lead pointers, it is required to traverse only once and flip the list element order overidea: This problem comes from the programming of the United States in the Headless list to remove the extension of the node, the page number is 228. The idea of this topic in front of the way to create a linked list, with a scan pointer through the list, the current scan pointer to the node pointed to the first node in the list, the implementation process has a key pointer variable, it always points to the first node of the list. The time complexity of this method is O (N), the space complexity is O (1), the code is as follows:
2. List reversal void Reverselist (Pnode phead) {//Only 1 nodes of the linked list is returned directly without reversing if (NULL = = Phead->pnext | | NULL = = Phead->pnext->pnext) {return;} Pnode p = phead->pnext;//Scan pointer pnode Pfirst = p;//always points to the first active node in the list pnode Ptempfirst = p;//Record the current first node address p = p->pnext;//Scan pointer Start directly from the 2nd node while (P! = NULL) {Pnode Tempnext = p->pnext;//The next node address referred to by the current node Phead->pnext = p;//The following steps insert the current node into the first node P- >pnext = Pfirst;pfirst = p;//Update PFIRSTP = tempnext;//Scan pointer Move}ptempfirst->pnext = null;//Reverse list last element closing}



3. Determine if two linked lists intersectTitle Description: to two single-linked lists, to determine whether they intersect, assuming that two linked lists are not with a ringidea: This problem comes from the beauty of programming 233 pages, if the two linked lists intersect, then they will be in a node at the beginning of a coincident, until the end is coincident, so a relatively simple way to judge is to traverse two linked list to the tail, compare whether the two nodes are the same node, This time complexity is O (| l1|+| l2|), L1,L2 represents two linked lists with the following code:
3. Determine if two linked lists intersect bool Isintersect (Pnode L1, Pnode L2) {if (NULL = = L1  | | NULL = = l2| | NULL = = L1->pnext| | NULL = = L2->pnext)//header pointer illegal, or empty linked list {return false;} Traverse L1, let L1 point to the last node while (l1->pnext! = NULL) {L1 = L1->pnext;} Traverse L2, let L2 point to the last node while (l2->pnext! = NULL) {L2 = L2->pnext;} Determine if the last node is the same return (L2 = = L1);}



4. How to check if a link list existsTopic Description: Given a single-linked list, how to check whether the chain list exists ringidea: If a single-linked list exists in the loop, if the direct traversal ignores an infinite loop, you can use two pointers, one called the slow pointer Pslow, each step forward, the other fast pointer pfast, can let it go forward two steps at a time, such as in the ring, it must be fast in the pursuit of slow, And will certainly meet, if the single-linked list does not have a ring, the final result must be pfast = null. The code is as follows:
4. How to check if the linked list exists ring bool Iscircle (Pnode phead) {pnode Pfast = Phead;pnode Pslow = phead;//If it is a single linked list without a ring, loop to Pfast==null or pfast- >pnext==null exit//If a loop is included, the loop exits with Pfast==pslow while (pfast! = NULL && Pfast->pnext! = null) {Pfast = pfast-> pnext->pnext;//Walk two steps pslow = pslow->pnext;//Walk One step if (Pslow = = Pfast) {break;}} return (Pfast = = Pslow);}



5. Find the length of the ring in the ring linked listTitle Description: Given a linked list with a ring, how to calculate its ring length is how muchidea: This is a typical pursuit problem, the key point when the speed pointer in the first encounter to the second encounter, the fast pointer than the slow pointer to run more than the distance is the length of the ring, this key point with the following figure can be clearly understood, fast pointer Orange multi-run distance minus the blue slow pointer running distance is the ring length. The code is as follows:

5. Finding the length of the ring in the ring list this function does not call the above band-ring function, is a separate write int getcirclelength (Pnode phead) {pnode Pfast = Phead;pnode Pslow = phead;// Loop to first encounter exits while (pfast! = NULL && Pfast->pnext! = null) {Pfast = pfast->pnext->pnext;//Walk two steps pslow = pslow- >pnext;//Take one step if (Pslow = = Pfast) {break;}} if (pslow! = pfast)//no ring {return 0;}  Starting from the first encounter//Ring length = N2-n1 = 2*n1-n1 = N1,n1 is the number of nodes that the slow pointer traversed int slowsteps = 0;do {pfast = pfast->pnext->pnext;//go two steps pslow = pslow->pnext;//Walk a step slowsteps++;} while (pslow! = pfast); return slowsteps;}


6. finding The ring starting point in a single linked list Title Description: Given a linked list with a ring, the starting point of the ring is obtainedidea: Still have to analyze from the first encounter, the amount of each distance, such as:


when Pfast and Pslow again S1 first meet, Pslow walk steps for s,pfast Walk step number is 2S, has the following relationship:
    • S = M + x ①
    • 2S = M + X + n*r (where n≧1, take integer) ②
②-①, get:S = n*r ③bring the ③ into the ① to get:n*r = m + X = + M = n*r-xfrom this equation we get what information, if from the beginning of the first encounter, the slow pointer and a head of the hands of the beginning to go, then the hands of the pointer to the S0, the slow pointer walked n*r-x distance, N*r said to go over the ring, the same as or S1, minus X after the S0, This means that the slow pointer and a head pointer begin to synchronize, they meet the point must be S0, that is, the beginning of the ring, get the request, see Code:
6. Find the ring connection point position of the ring single-linked list//This function does not call the above ring function, is written separately Pnode Getconnectionpoint (Pnode phead) {pnode Pfast = Phead;pnode Pslow = Phead ;//Loop to the first encounter exits while (pfast! = NULL && Pfast->pnext! = null) {Pfast = pfast->pnext->pnext;//Walk two steps pslow = pSl ow->pnext;//Take one step if (Pslow = = Pfast) {break;}} if (pslow! = pfast)//no ring {return NULL;} Make Pfast start from the beginning Pfast = Phead;while (pfast! = pslow) {pfast = Pfast->pnext;pslow = Pslow->pnext;} return pfast;}


7. If the linked list may have loops, determine if the two linked lists intersect and seek the first node to intersectTitle Description: Given two single-linked lists, it is possible to have a ring, how to determine whether the two linked lists intersect, and to obtain two linked lists intersect the first nodethought: This question only wrote the idea, did not put the code. Referring to some of the solutions on the Internet, the possible cases are divided into the following three kinds:
    1. One of the chain bands, one without the ring
    2. Two lists do not contain rings
    3. Two linked list with ring
In the first case, two linked lists will not intersect, because if one chain is broken and two linked lists intersect, the other linked list must have a ring. In the second case, if only two linked lists intersect, you can use the method of the 3 question directly, if the intersection is required, another approach is required. One of the linked list (recorded as L1) end-to-end, check whether the L2 band ring, if the band ring intersect, and according to the 5th question to find the intersection. For the third situation, as long as you know that a link on the ring is on another linked list, a link on the list of requirements, you can use the method of the previous fast and slow pointer, the method is used to remember the L1 in the ring on the point of POS1, this time can traverse L2, if the intersection, then there will be equal to POS1 node, But if you do not intersect, this traversal will become an infinite loop. You can use the same method to find the L2 in the ring on the Pos2, and then let the POS1 in the L1 traversal, before the next to POS1 to determine whether to encounter Pos2, if encountered then intersect, otherwise you can be judged as disjoint. In this case, the intersection of the two linked lists does not seem to be clearly defined. The whole problem to find the method to use the above methods, to determine whether there is no ring is the key, the code, you can use a few of the functions repeatedly used to package into a function, repeated calls, its logic and implementation of the idea is more clear.

Linked List Classic Exercises

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.