/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function(head, n) {
var dummy = new ListNode(-1);
dummy.next = head;
var fast = head;
var slow = head;
var pre = dummy;
for (; n > 0; n--) {
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
pre = slow;
slow = slow.next;
}
pre.next = slow.next;
return dummy.next;
};
没有评论:
发表评论