File tree 2 files changed +44
-1
lines changed 2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -28,4 +28,5 @@ public class Linkedlist {
28
28
1 . 使用给定的值去初始化新的节点cur;
29
29
2 . 插入链条当中。
30
30
31
- [ LeetCode876 题解] ( linkedlist/Leetcode876MiddleOftheLinkedList.md )
31
+ [ LeetCode876 题解] ( linkedlist/Leetcode876MiddleOftheLinkedList.md )
32
+ [ LeetCode19 题解] ( linkedlist/leetcode19.md )
Original file line number Diff line number Diff line change
1
+ 原题目是:
2
+ 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
3
+
4
+ 示例:
5
+
6
+ 给定一个链表: 1->2->3->4->5, 和 n = 2.
7
+
8
+ 当删除了倒数第二个节点后,链表变为 1->2->3->5.
9
+ 说明:
10
+
11
+ 给定的 n 保证是有效的。
12
+
13
+ [ 点击此处查看原题] ( https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/ )
14
+
15
+ 这道题比较简单,考了dummyNode在删除,添加节点的时候的使用。
16
+ 直接看答案:
17
+ ``` java
18
+ public ListNode removeNthFromEnd(ListNode head, int n) {
19
+ if (head == null ) return head;
20
+ // here is the typical dummy node usage.
21
+ ListNode node = new ListNode (- 1 );
22
+ node. next = head;
23
+ // find the delete position
24
+ int length = 0 ;
25
+ ListNode tmp = head;
26
+ while (tmp != null ) {
27
+ length++ ;
28
+ tmp = tmp. next;
29
+ }
30
+
31
+ length -= n;
32
+ tmp = node;
33
+ while (length > 0 ) {
34
+ length-- ;
35
+ tmp = tmp. next;
36
+ }
37
+ tmp. next = tmp. next. next;
38
+ return node. next;
39
+
40
+ }
41
+ ```
42
+
You can’t perform that action at this time.
0 commit comments