[leetcode linked list series] 2 Delete the node in the linked list

38c53560cb92b4175dee204888fee2fc.gif

Review the insertion of the linked list

    A node of the linked list is composed of a data field and a pointer field , and the address of the pointer field is the address of the next element. So how do we need to insert or delete an element?


821ca5ac31ba563737a2254d0fa208ce.png

  • First look at the original linked list structure and prepare to insert node x into the linked list.

d187cdbcd6c9dd6b5be14ea775143432.png


  • At this time, we need to save the address of node n (300) first, and store the address of node n in the pointer field of node m, and assign this value to the pointer field of node x. (x->next=m->next) becomes as shown in the figure below.

645589d2078ec7ae8f3bac507f1a8a04.png

  • At this time, connect the m node and the x node. m->next=x. As shown in the figure below.

b7e05c9201db3f264505b14e38eec651.png

Seeing this, some friends may have questions. If it is an empty list, this way of inserting will not work (you can draw a picture and see it), yes, that's the point to say, sentry .

  • What is the sentinel node?



We can first think about the reasons why the empty linked list cannot use the first scheme. Because it has no nodes, we naturally cannot get its address, so we add a head node. Then the structure of the empty linked list is as shown in Figure 4, non-empty linked list The structure is shown in Figure 5.

859e35e97f48783a455588efdb44f813.png

8d11f8715394d6d3738c2f34266a60a9.png

Review the deletion of the linked list

   

  • The above briefly introduces the linked list of the lead node, which is also applicable when deleting, so we will directly use the linked list of the lead node to explain in the future. Let's look directly at the delete node graph.


8f327118b68340c0dba251393d5376fd.png


1Leetcode237 delete the node of the linked list

Please write a function to delete a given (non-end) node in a linked list, and you will only be given the requested node to be deleted.


There is a linked list - head = [4,5,1,9], which can be expressed as:


9458b1a36c33f9405ad5ab7b6713a128.png






Example 1:

Input: head = [4,5,1,9], node = 5

Output: [4,1,9]

Explanation: Given the second node in your linked list with a value of 5, after calling your function, the linked list should become 4 -> 1 -> 9.

Description:

  • The linked list contains at least two nodes.

  • The values ​​of all nodes in the linked list are unique.

  • The given node is not the end node and must be a valid node in the linked list.

  • Don't return any results from your function.

Think about it for a minute!

The effect is better!


0 1 Question analysis

  • Conventional routine

    • Find the pointer of the previous node of the node to be deleted .

    • Point the pointer to the next node to be deleted. As shown in Figure 7 below.

2f8df2a4f204ec26cc2843f413f75d9c.png

  • Look carefully

    • Found that, the title did not tell us the previous node, it doesn't matter, we can find a way to construct a node, please take a look.

    • The goal is to delete 5, and the final result is [4,1,9]. We assign the node 1 behind the 5 nodes that need to be deleted to it, as shown in Figure 8.

2f5ede3d4def9bd14c96912fef6c31e7.png

  • Hey, now the value of two nodes is 1. No matter which one is deleted, we can get the result, but the second node 1 is not convenient for us to delete, but the third node 1 is still easy. Assuming that the p pointer points to the deleted node, it is directly p.next=p.next.next. As shown in Figure 9.

ff8cdd85a32071bcd4c7e8bc2ac24f70.png

0 2 Code implementation

1c++ version

c38dca1e04b7e83441ed08bb989dcef5.jpeg

2python version

24005edf8c54846ff0bb8ee38b55b683.jpeg

3java version

ff8b143f9ad223a63d087c33f9b3a78e.jpeg


Guess you like

Origin blog.51cto.com/14984904/2545479