Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9d020c2

Browse files
committed
add messages
1 parent 28c172c commit 9d020c2

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

linkedlist/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ public class Linkedlist {
2828
1. 使用给定的值去初始化新的节点cur;
2929
2. 插入链条当中。
3030

31-
[LeetCode876 题解](linkedlist/Leetcode876MiddleOftheLinkedList.md)
31+
[LeetCode876 题解](linkedlist/Leetcode876MiddleOftheLinkedList.md)
32+
[LeetCode19 题解](linkedlist/leetcode19.md)

linkedlist/leetcode19.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+

0 commit comments

Comments
 (0)