Given a linked list, remove the nth node from the end of list and return its head.
Example
Given linked list: 1->2->3->4->5->null
, and n = 2
.
After removing the second node from the end, the linked list becomes 1->2->3->5->null
.
class Solution:
"""
@param head: The first node of linked list.
@param n: An integer
@return: The head of linked list.
"""
def removeNthFromEnd(self, head, n):
res = ListNode(0)
res.next = head
p1 = res
for i in range(0,n):
head = head.next
while head != None:
head = head.next
p1 = p1.next
p1.next = p1.next.next
return res.next