Summary of Chain list algorithm

Source: Internet
Author: User

The code may be forgotten, but if the idea is still there, we can write the code along the line of thought, so here's a summary of the idea; here, I'm primarily a porter and then I'm adding some of my own understanding.

1: title Description: the node is deleted at O (1) Time given the head pointer of the list and a node pointer. [Google face question]

Idea: To delete a linked list node we generally want to find the node's precursor node: And this topic gives us a current node pointer, obviously to the O (1) Time to delete the node, so we can only take this node pointer as a precursor node, so to delete is the successor node. But our goal is to delete the node that the current pointer refers to.

WORKAROUND: Assign the values of the subsequent nodes to the current node, and then delete the subsequent nodes.

1 //O (1) time delete a linked list node, remove a node from the headless single-linked list2 voidDeleterandomnode (Node *cur)3 {4ASSERT (cur! =NULL);5ASSERT (Cur->next! = NULL);//cannot be a tail node6node* Pnext = cur->Next;7Cur->data = pnext->data;8Cur->next = pnext->Next;9 Delete pnext;Ten}

topic two : Reversal of single-linked list

The basic single-linked list of Flip has three ways: one is the non-recursive way (direct traversal) and the other is the use of head interpolation method to reverse the single-linked list, the third is the use of recursive way to reverse.

Mode 1:

Idea: The general idea is simple is to traverse the single-linked list of the first node pointer to empty, the second node point to the first node, the third node point to the second node, and so on (here you need to define a pointer, a pointer to the current node node* cur a Pointer to the predecessor node of the current node node* pre).

It is also important to note that the point is that when we point the second node to the first node, or the third node to the second node, the list is broken, in order to prevent the broken list, we need to define a pointer to record the next node of the current node. (Here you need to define a successor node for the current node: node* next) forms two lists after each operation the pre points to the last node of the first list (the last node is because I'm describing it based on the location of the original linked list, It can also be said that the first node of the new list) head points to the first node of the second list, the core operation is as follows

1 node* pre = NULL; 2 node* next = NULL; 3 next = head->4 head->next = pre; 5 pre = head; // because you want to traverse the entire single-linked list, the pre and head are followed up with the head at the current node.  6 head = Next;

The following is a specific code:

 //  transpose of single-linked list, traverse method  node* Reversebyloop (Node *head) { if  (head = = NULL | | Head->next == NULL)  return   head;    Node  *pre = NULL;    Node  *next = NULL;  while  (head!= NULL) {Next  = he        Ad->next;        Head ->next = pre;        Pre  = head;    Head  = next;  return   pre;}  

Method Two: The head insertion method is to take the single-linked list down, in front of the insertion method, the first node down in the first position, the second node is removed to plug in front of the first node, the third node is removed to insert the front of the second node (that is, the whole front) and so on. The specific code execution process is as follows:

1 //each time a node is taken from a linked list, it is inserted in the head of the list. To achieve the reversal of the list here is the case that the linked list already exists2node* Reversebyinsertf (node*p)3 {4node* pre = NULL, *cur =p;5      while(cur)6     {7node* next = cur->next;//This is because it is feared that each time a node is connected to the chain in front of the list of broken chain, so first cache the node to be removed next node8Cur->next = pre;//each time the first node of the remaining list (that is, the current node) is removed, the first node of the linked list is reversed every time. 9Pre = cur;//where pre points to the first node of the linked list after the reversalTenCur =Next; One     } A          -}

Method Three: Adopt the Recursive way:

Idea: Reverse the next node before reversing the current node the specific process is as follows

  

1 //transpose of single-linked list, recursive method2node* reversebyrecursion (Node *head)3 {4     //The first condition is to judge an exception, the second condition is to end the judgment5     if(head = = NULL | | head->next = =NULL)6         returnhead;7Node *newhead = reversebyrecursion (head->next);8Head->next->next =head;9Head->next =NULL;Ten     returnNewhead;//returns the head pointer of a new linked list One}

Summary of Chain list algorithm

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.