链表中的节点个数大于等于n
给出链表1->2->3->4->5->null和 n = 2.
删除倒数第二个节点之后,这个链表将变成1->2->3->5->null
ListNode removeNthFromEnd(ListNode head, int n) {
// write your code here
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode fast = dummy;
ListNode slow = dummy;
while (fast != null && n > -1) {
fast = fast.next;
n--;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
来源:CSDN
作者:哎呦、不错哦
链接:https://blog.csdn.net/l1394049664/article/details/81369474