Delete a node in a single-linked list

Source: Internet
Author: User

Recently saw Linus in the interview mentioned a way to delete nodes in a single-linked list, and said that people do not understand the method of the pointer, the exact words are as follows:

At the opposite end of the spectrum, I actually wish more people understood the really core low-level kind of coding. Not big, complex stuff like the lockless name lookup, but simply good use of pointers-to-pointers etc. For example, I ' ve seen too many people who delete a singly-linked list entry by keeping track of the "prev" entry, and the N to delete the entry, doing something like

if (prev)
Prev->next = entry->next;
Else
List_head = entry->next;

And whenever I see code like that, I just go "this person doesn ' t understand pointers". And it ' s sadly quite common.


People who understand pointers just use a "pointer to the entry pointer", and initialize this with the address of the list _head. And then as they traverse the list, they can remove the entry without using any conditionals, by just doing a "*PP = entry ->next ".

So there's lots of pride in doing, the small details right. It may isn't being big and important code, but I don't like seeing code where people really thought about the details, and clearly Also were thinking about the compiler being able to generate efficient code (rather than hoping that the compiler are so s Mart that it can make efficient code *despite* the state of the original source code).

In fact, the great God said the advantage of this method is: when deleting a node, do not distinguish whether the node is the first node of the list, the implementation code is as follows:

1 //define the simplest list structure2typedefstructnode3 {4     intvalue;5     structnode*Next;6 }node;7  8node** pp = &List_head;9node* entry =List_head;Ten   One //Suppose the value of the node to be deleted is 9 A  while(Entry->value! =9) - { -     //each iteration saves the address of the next variable in pp the     //Next is the pointer variable, so its address is the address, that is, node** -PP = &entry->Next; -Entry = entry->Next; - } +   - //point the next value of the predecessor node to the next node +*PP = entry->next;

The contents of each variable are changed as follows:

Memory address: 0x1               0x2                       0x4              0x8        list_head=0x2     node1 (value:10,   node2 (Value:9, Node3 (Value:8)                                memory address of next variable:                                0x3                       0x5              0x9                                next:0x4)                 next:0x8         next:null iterative process: pp=0x1entry=0x2 pp= 0x3entry=0x4 Find the node to delete: *pp=0x8 = 0x2                       node1 (value:10,                 0x3                       next:0x4 = 0x8)

Thus, regardless of whether the Node1 (head node) or normal node is to be deleted, it is not necessary to determine if the predecessor node is a pointer to the head node.

Delete a node in a single-linked list

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.