题目
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
示例 1:
输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
解
class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {ListNode dummy = new ListNode(0);dummy.next = head;ListNode slow = dummy;ListNode fast = dummy;for(int i = 0;i < n;i++){fast = fast.next;}while(fast.next != null){slow = slow.next;fast = fast.next;}slow.next = slow.next.next;return dummy.next;}
}