给你一个链表,删除链表的倒数第 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;
}
}
标签:node,head,ListNode,next,链表,lc19,second,first From: https://www.cnblogs.com/somedieyoung/p/16734911.html