/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode nhead=new ListNode(-1);//删除链表用头插法比较好,避免空指针
nhead.next=head;
int count=0;
ListNode curr=head;
while(curr!=null){
curr=curr.next;
count++;
}
ListNode curr1=nhead;
int s=count-n;
while(s>0){
curr1=curr1.next;
s--;
}
curr1.next=curr1.next.next;
return nhead.next;
}
}
LeetCode19删除链表的倒数第N个节点
最新推荐文章于 2024-05-03 10:09:31 发布