Linked list Advanced algorithm--2

Source: Internet
Author: User

first, determine whether the two linked lists intersect

int Checkcross (plinklist list1, plinklist list2)   //Determine if the linked list intersects {assert (List1); assert (List2);p linknode cur1 = list1- >phead;plinknode CUR2 = list2->phead;if ((null==list1->phead) | | (Null==list2->phead)) {return-1;} while (null!=cur1->next) {cur1 = Cur1->next;} while (null!=cur2->next) {cur2 = Cur2->next;} if (Cur1 = = cur2) {return  1;                Intersect}else{return-1;                 Do not intersect}}




Ii. merging of two ordered linked listsThere are a lot of ways to merge two sequential lists, and the following is a common Method!!!
algorithm: First create a virtual head node, the two linked list as two ordered collection, each time from the two sets of the first node to compare, the smaller values from the collection to link to the virtual node, Then use a pointer last to record the end of this linked list of virtual nodes, so repeatedly, until one of the collections is empty, and then all the nodes in the other collection are linked to the last.
Plinklist merge (Plinklist list1, plinklist list2)//merge two ordered list {assert (List1); assert (List2); if ((NULL = = List1->phead) && (NULL = = List2->phead))//Consider special case {return NULL;} else if (NULL = = List1->phead) {return list2;} else if (NULL = = List2->phead) {return list1;}                       Plinknode last = Null;plinknode Newhead = NULL; Virtual head node if (list1->phead->data <list2->phead->data)//treat the first node with a special treatment {newhead= list1->phead;list1- >phead = List1->phead->next;} Else{newhead = List2->phead;list2->phead = List2->phead->next;}                                    Last=newhead; Let last point to the tail of the virtual node list while ((null!= list1->phead) && (null!= list2->phead)) {if (list1->phead->data <list2->phead->data) {Last->next = List1->phead;list1->phead = List1->phead->next;} Else{last->next= list2->phead;list2->phead = List2->phead->next;} last = Last->next;} if (NULL = = List1->phead)//will be left in the collectionAfter the element is linked to last {last->next = List2->phead;} Else{last->next = List1->phead;} List1->phead = Newhead;return List1;}





third, determine whether a linked list with a ring, if the band ring back to the location of the meeting pointThis problem needs to be applied to the speed pointer to solve, fast pointer fast each walk two steps, slow hands each step. Because fast each time is more than slow one step, if the chain bracelet, then eventually fast will definitely meet with slow.
Plinknode checkcycle (plinklist list)             //To determine if the band ring, if the band is returned to the encounter position {assert (list);p Linknode fast = list->phead;                 Quick pointer Plinknode slow = list->phead;                 Slow pointer if (null==list->phead) {return NULL;} while (fast!= null) {if (null! = Fast->next) {fast = Fast->next->next;slow = Slow->next;} Else{break;} if (fast = = slow)                   //The speed pointer encounters a ring {return slow;}} return NULL;                            No ring returns null}




Iv. Finding the length of the ring in the ring-linked listThis problem is relatively simple, the third problem has found the speed of the pointer in the ring in the meeting point, we only need to set a count, from this point of encounter to traverse the loop on the line.
int  getcirclelength (Plinknode meet)         //If chain strap ring This returns the length of the ring {assert (meet);p linknode cur = meet->next;int count = 1; while (cur!=meet) {count++;cur = Cur->next;} return count;}





v. Finding the entry point of the ring in the ring linked listTo solve this problem we have a theorem, with two pointers, from the head of the list and the meeting point in the ring, after a number of steps, the two pointers will meet at the entrance of the ring.               
Plinknode Getcycleentrynode (plinklist list, plinknode meet)   //Lookup ring Entry {assert (list);p Linknode link = list->phead ;             Link points to the head of the list plinknode cycle = meet;                   Cycle points to the meeting point of the list while (link! = cycle) {link = link->next;cycle = cycle->next;} return link;}


Linked list Advanced algorithm--2

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.