lc19 removeNthFromEnd(node) 链表删除
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
示例 1:
输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
示例 2:
输入:head = [1], n = 1
输出:[]
class Solution{
public ListNode removeNthFromTheEnd(LlistNode head,int n){
ListNode dummy=Listnode head(-1,0);
ListNode first=dummy;
ListNode second=head;
for(int i=0;i<n;i++){
second=second.next;}
while(second!=null){
first=first.next;
second=second.next;}
first.next=first.next.next;
ListNode output=dummy.next;
return output;
}
}
posted on 2022-09-27 16:04 somedieyoung-navi 阅读(15) 评论(0) 编辑 收藏 举报