7, linked list (bottom): How to easily write the correct link list code?

Source: Internet
Author: User

Very embarrassed, now let oneself complete write a simple list structure unexpectedly will not be able to pen, as a program ape, need to practice the road is very long.

Tip: Understand the meaning of a pointer or reference

Assigning a variable to a pointer is actually assigning the address of the variable to the pointer, or, conversely, the memory address of the variable stored in the pointer, pointing to the variable, which can be found by pointers.

Example: P->next = Q. This line of code means that the next pointer of the P node stores the memory address of the Q node.

Similarly, P->next = P->next->next. This line of code means that the next pointer to the P node stores the address of the next node of the P node.

Tip Two: Beware of pointer loss and memory leaks

For example, insert a node:

, we want to insert node x between nodes A and B, assuming that the current pointer p points to node A. If the code follows, pointer loss and memory leaks occur.

`P->next = x;x`

Here, after P->next executes, its pointer no longer points to B, but instead points to X. The second line of code is the equivalent of pointing to yourself. As a result, the list is broken into two halves, and all definitions from Node B are inaccessible.

Correct way: The first line of code and the second line of code are reversed in order to be OK.

Technique three: Using Sentinel to simplify the implementation of difficulty

As below, node p is inserted after the correct code for the node.

`X->next = p->next;p->next = x;`

Note: When you need to insert the first node into an empty list, the logic above is not available. The correct answers are as follows:

`if (head==null) {    = new_node;}`

Similarly, when you write a delete operation for a linked list, there are two situations:

First, the deletion node is not the tail node, but the subsequent node of the P node

`P->next = p->next->next;`

Second, the deletion node is the tail node.

`if NULL ) {   null;  }`

tip Four: Focus on boundary condition processing

If the linked list is empty, does the code work correctly?

If the list contains only one node, does the code work correctly?

If a linked list contains only two nodes, does the code work correctly?

Does the code logic work correctly when dealing with head and tail nodes?

Technique five: Draw your own picture, assist the memory

Common list operations:

Second, the chain list of the detection of the ring

Iii. merging of two ordered linked lists

Iv. Delete the last nth node of the linked list

V. To find the middle node of the chain list

Corresponding to the Leetcode exercises number: 206,141,21,19,876.

After-school thinking: Can you think of other scenarios where using sentinels greatly simplifies coding difficulty?

Follow up on the implementation of their own five kinds of linked list operations to pass up. Please correct me!

7, linked list (bottom): How to easily write the correct list code?

Related Keywords:

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.