19:删除链表的倒数第 N 个结点
This commit is contained in:
parent
e4d436199e
commit
a3768edee8
@ -0,0 +1,84 @@
|
|||||||
|
//给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
|
||||||
|
//
|
||||||
|
// 进阶:你能尝试使用一趟扫描实现吗?
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:head = [1,2,3,4,5], n = 2
|
||||||
|
//输出:[1,2,3,5]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:head = [1], n = 1
|
||||||
|
//输出:[]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:head = [1,2], n = 1
|
||||||
|
//输出:[1]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 链表中结点的数目为 sz
|
||||||
|
// 1 <= sz <= 30
|
||||||
|
// 0 <= Node.val <= 100
|
||||||
|
// 1 <= n <= sz
|
||||||
|
//
|
||||||
|
// Related Topics 链表 双指针
|
||||||
|
// 👍 1398 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.ListNode;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//19:删除链表的倒数第 N 个结点
|
||||||
|
public class RemoveNthNodeFromEndOfList{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new RemoveNthNodeFromEndOfList().new Solution();
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
/**
|
||||||
|
* Definition for singly-linked list.
|
||||||
|
* public class ListNode {
|
||||||
|
* int val;
|
||||||
|
* ListNode next;
|
||||||
|
* ListNode() {}
|
||||||
|
* ListNode(int val) { this.val = val; }
|
||||||
|
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
class Solution {
|
||||||
|
public ListNode removeNthFromEnd(ListNode head, int n) {
|
||||||
|
List<ListNode> list = new ArrayList<>();
|
||||||
|
ListNode temp = head;
|
||||||
|
while (temp != null) {
|
||||||
|
list.add(temp);
|
||||||
|
temp = temp.next;
|
||||||
|
}
|
||||||
|
if (n == list.size() && head != null) {
|
||||||
|
head = head.next;
|
||||||
|
} else if (list.size() != 1) {
|
||||||
|
list.get(list.size() - n - 1).next = list.get(list.size() - n).next;
|
||||||
|
}
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
<p>给你一个链表,删除链表的倒数第 <code>n</code><em> </em>个结点,并且返回链表的头结点。</p>
|
||||||
|
|
||||||
|
<p><strong>进阶:</strong>你能尝试使用一趟扫描实现吗?</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style="width: 542px; height: 222px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>head = [1,2,3,4,5], n = 2
|
||||||
|
<strong>输出:</strong>[1,2,3,5]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>head = [1], n = 1
|
||||||
|
<strong>输出:</strong>[]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>head = [1,2], n = 1
|
||||||
|
<strong>输出:</strong>[1]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>链表中结点的数目为 <code>sz</code></li>
|
||||||
|
<li><code>1 <= sz <= 30</code></li>
|
||||||
|
<li><code>0 <= Node.val <= 100</code></li>
|
||||||
|
<li><code>1 <= n <= sz</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>链表</li><li>双指针</li></div></div>\n<div><li>👍 1398</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user