# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val)
# @val = val
# @next = nil
# end
# end
# @param {ListNode} head
# @return {ListNode}
def remove_nth_from_end(head, n)
d =ListNode.new(0)
d.next = head
a = b = d
n.times {b = b.next}
while b.next
a = a.next
b = b.next
end
a.next = a.next.next
return d.next
end
Given a linked list, remove the n th node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.