给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

输入:
head = [1,2,3,4,5], n = 2
输出:
[1,2,3,5]
输入:
head = [1], n = 1
输出:
[]
输入:
head = [1,2], n = 1
输出:
[1]
sz1 <= sz <= 300 <= Node.val <= 1001 <= n <= szhttps://leetcode.cn/problems/remove-nth-node-from-end-of-list/
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, mut n: i32) -> Option<Box<ListNode>> {
let mut dummy = Some(Box::new(ListNode::new(-1)));
dummy.as_mut().unwrap().next = head;
let mut slow = &mut dummy;
let mut fast = &slow.clone();
while n >= 0 {
if let Some(n) = fast {
fast = &n.next;
} else {
return None;
}
n -= 1;
}
while fast.is_some() {
slow = &mut slow.as_mut().unwrap().next;
fast = &fast.as_ref().unwrap().next;
}
slow.as_mut().unwrap().next = slow.as_mut().unwrap().next.as_mut().unwrap().next.take();
return dummy.unwrap().next;
}
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func removeNthFromEnd(head *ListNode, n int) *ListNode {
dummy := &ListNode{0, head}
fast, slow := head, dummy
for i := 0; i < n; i++ {
fast = fast.Next
}
for ; fast != nil; fast = fast.Next {
slow = slow.Next
}
slow.Next = slow.Next.Next
return dummy.Next
}
/**
* 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) {
ListNode dummy = ListNode(0, head);
ListNode *fast = head;
ListNode *slow = &dummy;
for (int i = 0; i < n; ++i) {
fast = fast->next;
}
while (fast) {
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
return dummy.next;
}
};
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy = ListNode(0, head)
fast = head
slow = dummy
for i in range(n):
fast = fast.next
while fast:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy.next
/**
* 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 dummy = new ListNode(0, head);
ListNode fast = head;
ListNode slow = dummy;
for (int i = 0; i < n; ++i) {
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}
非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~