1
- #19. Remove Nth Node From End of List - Best Practices of LeetCode Solutions
2
- LeetCode link:[ 19. Remove Nth Node From End of List] ( https://leetcode.com/problems/remove-nth-node-from-end-of-list ) ,
3
- [ 19. 删除链表的倒数第 N 个结点] ( https://leetcode.cn/problems/remove-nth-node-from-end-of-list )
1
+ #19. 删除链表的倒数第 N 个结点 - 力扣题解最佳实践
2
+ 力扣链接:[ 19. 删除链表的倒数第 N 个结点] ( https://leetcode.cn/problems/remove-nth-node-from-end-of-list ) , 难度:** 中等** 。
4
3
5
- [ 中文题解] ( #中文题解 )
6
-
7
- ##LeetCode problem description
8
- Given the` head ` of a linked list, remove the` n-th ` node from the end of the list and return_ its head_ .
4
+ ##力扣“19. 删除链表的倒数第 N 个结点”问题描述
5
+ 给你一个链表,删除链表的倒数第` n ` 个结点,并且返回链表的头结点。
9
6
10
- ###[ Example 1]
7
+ ###[ 示例 1]
11
8
![ ] ( ../../images/examples/19_1.jpg )
12
9
13
- ** Input ** :` head = [1,2,3,4,5], n = 2 `
10
+ ** 输入 ** :` head = [1,2,3,4,5], n = 2 `
14
11
15
- ** Output ** :` [1,2,3,5] `
12
+ ** 输出 ** :` [1,2,3,5] `
16
13
17
- ###[ Example 2]
18
- ** Input ** :` head = [1], n = 1 `
14
+ ###[ 示例 2]
15
+ ** 输入 ** :` head = [1], n = 1 `
19
16
20
- ** Output ** :` [] `
17
+ ** 输出 ** :` [] `
21
18
22
- ###[ Example 3]
23
- ** Input ** :` head = [1,2], n = 1 `
19
+ ###[ 示例 3]
20
+ ** 输入 ** :` head = [1,2], n = 1 `
24
21
25
- ** Output ** :` [1] `
22
+ ** 输出 ** :` [1] `
26
23
27
- ###[ Constraints ]
28
- - The number of nodes in the list is ` sz ` .
24
+ ###[ 约束 ]
25
+ - 链表中结点的数目为 ` sz `
29
26
- ` 1 <= sz <= 30 `
30
27
- ` 0 <= Node.val <= 100 `
31
28
- ` 1 <= n <= sz `
32
29
30
+ ###[ 提示]
33
31
<details >
34
- <summary >Hint 1</summary >
35
- Maintain two pointers and update one with a delay of n steps.
32
+ <summary >提示 1</summary >
33
+ Maintain two pointers and update one with a delay of n steps.
36
34
</details >
37
35
38
- ##Intuition
39
- [ 中文题解] ( #中文题解 )
40
-
41
- 1 . Deleting the` N-th ` to last node in a linked list is equivalent to deleting the` node_count - N ` -th node in the linked list.
42
- 2 . First find out` node_count ` .
43
- 3 . When` index == node_count - N ` , delete the node by` node.next = node.next.next ` .
44
- 4 . Since the deleted node may be` head ` , a virtual node` dummy_node ` is used to facilitate unified processing.
36
+ ##思路
37
+ 1 . 删除链表的倒数第` N ` 个结点,等同于删除链表的第` node_count - N ` 个结点。
38
+ 2 . 先求出` node_count ` 。
39
+ 3 . 在` index == node_count - N ` 时,进行删除节点操作:` node.next = node.next.next ` 。
40
+ 4 . 由于删除的节点可能是` head ` ,所以使用虚拟节点` dummy_node ` ,方便统一处理。
45
41
46
- ##Steps to the Solution
47
- 1 . First find out ` node_count ` .
42
+ ##步骤
43
+ 1 . 求出 ` node_count ` 。
48
44
``` ruby
49
45
node_count= 0
50
46
node= head
@@ -55,7 +51,7 @@ while node
55
51
end
56
52
```
57
53
58
- 2 . When ` index == node_count - N ` , delete the node by ` node.next = node.next.next ` .
54
+ 2 . 在 ` index == node_count - N ` 时,进行删除节点操作: ` node.next = node.next.next ` 。
59
55
``` ruby
60
56
index= 0
61
57
node= head
@@ -71,7 +67,7 @@ while node
71
67
end
72
68
```
73
69
74
- 3 . Since the deleted node may be ` head ` , a virtual node ` dummy_node ` is used to facilitate unified processing.
70
+ 3 . 由于删除的节点可能是 ` head ` ,所以使用虚拟节点 ` dummy_node ` ,方便统一处理。
75
71
``` ruby
76
72
dummy_head= ListNode .new # 1
77
73
dummy_head.next= head# 2
@@ -82,9 +78,9 @@ node = dummy_head # 3
82
78
return dummy_head.next
83
79
```
84
80
85
- ##Complexity
86
- * Time: ` O(n) ` .
87
- * Space: ` O(1) ` .
81
+ ##复杂度
82
+ * 时间: ` O(N) ` 。
83
+ * 空间: ` O(1) ` 。
88
84
89
85
##Java
90
86
``` java
380
376
```
381
377
// Welcome to create a PR to complete the code of this language, thanks!
382
378
```
383
-
384
- ##问题描述
385
- 给你一个链表,删除链表的倒数第` n ` 个结点,并且返回链表的头结点。
386
-
387
- ###[ Example 1]
388
- ![ ] ( ../../images/examples/19_1.jpg )
389
-
390
- ** 输入** :` head = [1,2,3,4,5], n = 2 `
391
-
392
- ** 输出** :` [1,2,3,5] `
393
-
394
- #中文题解
395
- ##思路
396
- 1 . 删除链表的倒数第` N ` 个结点,等同于删除链表的第` node_count - N ` 个结点。
397
- 2 . 先求出` node_count ` 。
398
- 3 . 在` index == node_count - N ` 时,进行删除节点操作:` node.next = node.next.next ` 。
399
- 4 . 由于删除的节点可能是` head ` ,所以使用虚拟节点` dummy_node ` ,方便统一处理。
400
-
401
- ##步骤
402
- 1 . 求出` node_count ` 。
403
- ``` ruby
404
- node_count= 0
405
- node= head
406
-
407
- while node
408
- node_count+= 1
409
- node= node.next
410
- end
411
- ```
412
-
413
- 2 . 在` index == node_count - N ` 时,进行删除节点操作:` node.next = node.next.next ` 。
414
- ``` ruby
415
- index= 0
416
- node= head
417
-
418
- while node
419
- if index== node_count- n
420
- node.next= node.next.next
421
- break
422
- end
423
-
424
- index+= 1
425
- node= node.next
426
- end
427
- ```
428
-
429
- 3 . 由于删除的节点可能是` head ` ,所以使用虚拟节点` dummy_node ` ,方便统一处理。
430
- ``` ruby
431
- dummy_head= ListNode .new # 1
432
- dummy_head.next= head# 2
433
- node= dummy_head# 3
434
-
435
- # omitted code
436
-
437
- return dummy_head.next
438
- ```