Skip to content

Commit 29c3f5c

Browse files
author
zhangbo54
committed
19. Remove Nth Node From End of List
1 parent b1eac82 commit 29c3f5c

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

src/leetcode/_19_/ListNode.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode._19_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-06.
5+
*/
6+
public class ListNode {
7+
int val;
8+
ListNode next;
9+
10+
ListNode(int x) {
11+
val = x;
12+
}
13+
14+
public int getVal() {
15+
return val;
16+
}
17+
18+
public void setVal(int val) {
19+
this.val = val;
20+
}
21+
22+
public ListNode getNext() {
23+
return next;
24+
}
25+
26+
public ListNode setNext(ListNode next) {
27+
this.next = next;
28+
return this;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
StringBuilder sb = new StringBuilder();
34+
ListNode head = this;
35+
while (head != null) {
36+
sb.append(head.val).append("->");
37+
head = head.next;
38+
}
39+
sb.delete(sb.length() - 2, sb.length());
40+
return sb.toString();
41+
}
42+
}

src/leetcode/_19_/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package leetcode._19_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
ListNode node = new ListNode(1).setNext(new ListNode(2).setNext(new ListNode(3).setNext(new ListNode(4).setNext(new ListNode(5)))));
9+
// ListNode node = new ListNode(1).setNext(new ListNode(2));
10+
Solution solution = new Solution();
11+
ListNode listNode = solution.removeNthFromEnd(node, 2);
12+
System.out.println(listNode.toString());
13+
}
14+
}
15+

src/leetcode/_19_/Solution.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package leetcode._19_;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Set;
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* public class ListNode {
13+
* int val;
14+
* ListNode next;
15+
* ListNode(int x) { val = x; }
16+
* }
17+
*/
18+
class Solution {
19+
public ListNode removeNthFromEnd(ListNode head, int n) {
20+
ListNode node1 = head;
21+
ListNode node2 = head;
22+
// 由于题目中说到 n 永远有效,所以不做 nodeList 长度不足n的判断
23+
for (int i = 0; i < n; i++) {
24+
node1 = node1.next;
25+
}
26+
if (node1 == null) {
27+
return head.next;
28+
}
29+
node1 = node1.next;
30+
31+
while (node1 != null) {
32+
node1 = node1.next;
33+
node2 = node2.next;
34+
}
35+
node2.next = node2.next.next;
36+
return head;
37+
}
38+
}

src/leetcode/_19_/solution.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
### [19\. Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a linked list, remove the _n_-th node from the end of list and return its head.
7+
8+
**Example:**
9+
10+
```
11+
Given linked list: 1->2->3->4->5, and n = 2.
12+
13+
After removing the second node from the end, the linked list becomes 1->2->3->5.
14+
```
15+
16+
**Note:**
17+
18+
Given _n_ will always be valid.
19+
20+
**Follow up:**
21+
22+
Could you do this in one pass?
23+
24+
25+
#### Solution
26+
27+
Language: **Java**
28+
29+
```java
30+
/**
31+
* Definition for singly-linked list.
32+
* public class ListNode {
33+
* int val;
34+
* ListNode next;
35+
* ListNode(int x) { val = x; }
36+
* }
37+
*/
38+
class Solution {
39+
   public ListNode removeNthFromEnd(ListNode head, int n) {
40+
       ListNode node1 = head;
41+
       ListNode node2 = head;
42+
       // 由于题目中说到 n 永远有效,所以不做 nodeList 长度不足n的判断
43+
       for (int i = 0; i < n; i++) {
44+
           node1 = node1.next;
45+
      }
46+
       if (node1 == null) {
47+
           return head.next;
48+
      }
49+
       node1 = node1.next;
50+
51+
       while (node1 != null) {
52+
           node1 = node1.next;
53+
           node2 = node2.next;
54+
       }
55+
       node2.next = node2.next.next;
56+
       return head;
57+
  }
58+
}
59+
           node2 = node2.next;
60+
```
61+
![](https://ws3.sinaimg.cn/large/006tKfTcgy1g0ugblbxe4j310o0u0dk3.jpg)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy