LeetCode19. 删除链表的倒数第 N 个结点🌟🌟🌟🌟🌟中等
课后作业
问题描述
原文链接:19. 删除链表的倒数第 N 个结点
给你一个链表,删除链表的倒数第 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 <= 300 <= Node.val <= 1001 <= n <= sz
Java
/**
* 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) {
if(head == null){
return head;
}
int k = 0;
ListNode temp = head;
while(temp != null){
k++;
temp = temp.next;
}
// index
int index = k - n + 1;
if(index == 1){
return head.next;
}
ListNode pre = head;
while(index > 2){
index --;
pre = pre.next;
}
pre.next = pre.next.next;
return head;
}
}
Python
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
if not head:
return head
k = 0
temp = head
while temp:
k += 1
temp = temp.next
# index
index = k - n + 1
if index == 1:
return head.next
pre = head
while index > 2:
index -= 1
pre = pre.next
pre.next = pre.next.next
return head
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(!head){
return head;
}
int k = 0;
ListNode* temp = head;
while(temp){
k++;
temp = temp->next;
}
// index
int index = k - n + 1;
if(index == 1){
return head->next;
}
ListNode* pre = head;
while(index > 2){
index --;
pre = pre->next;
}
pre->next = pre->next->next;
return head;
}
};
Go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func removeNthFromEnd(head *ListNode, n int) *ListNode {
if head == nil {
return head
}
k := 0
temp := head
for temp != nil {
k++
temp = temp.Next
}
// index
index := k - n + 1
if index == 1 {
return head.Next
}
pre := head
for index > 2 {
index--
pre = pre.Next
}
pre.Next = pre.Next.Next
return head
}
评论(9)
和上一题差不多,加个虚拟头结点统一操作,不计入今天的刷题
”’
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode*dummyHead=new ListNode(0);
dummyHead->next=head;
};
”’
/**
* 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) {
// ListNode cnt = head;
// int count = 2;
// if(cnt.next null) return null;
// while(cnt.next.next != null) {
// count++;
// cnt = cnt.next;
// }
// if(n 1) {
// cnt.next = null;
// return head;
// }
// int i = count – n;
// if(i 0) {
// return head = head.next;
// }
// cnt = head;
// while(i > 1) {
// cnt = cnt.next;
// i–;
// }
// cnt.next = cnt.next.next;
// return head;
// }
// }
// 【思路】双指针
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode pre = head;
ListNode sub = head;
if(sub.next null) return null;
while(sub.next != null) {
sub = sub.next;
n–;
if(n > -1) {
continue;
}
pre = pre.next;
}
if(pre head && n 1) {
return head.next;
}
pre.next = pre.next.next;
return head;
}
}
方法一:快慢指针(不使用虚拟头结点)
方法二:统计节点个数再定位删除
快慢指针,不使用虚拟头结点
index 1记得考虑
打卡