public static ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null) {
return null;
}
ListNode predel = null,del = null, cur =head;
int count = 0;
while(cur!=null) {
count++;
if(del !=null) {
predel = del;
del = del.next;
}
if(count == n) {
del = head;
}
if(count == n+1) {
predel = head;
}
cur = cur.next;
}
if(predel == null && del !=null) {
head = head.next;
}else if(predel !=null && del !=null) {
predel.next = del.next;
}
return head;
}
19. Remove Nth Node From End of List
最新推荐文章于 2024-10-13 15:10:00 发布