Remove Nth Node From End of List
Given a linked list, remove the nth 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.
先找到第n个数 然后2个指针一个指第n个数 一个指第0个数然后两个指针一起便利 直到第一个指针到达队尾 第二个指针与第一个指针距离为n 即删掉第二个指针后面的节点即可
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param {ListNode} head
# @param {integer} n
# @return {ListNode}
def removeNthFromEnd(self, head, n):
s = head
e = head
l = n
while l > 0:
l = l - 1
s = s.next
if s == None:
return head.next
while s.next !=None:
s = s.next
e = e.next
e.next = e.next.next
return head