# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
#
# @param head ListNode类
# @param n int整型
# @return ListNode类
#
class Solution:
def removeNthFromEnd(self , head , n ):
# write code here
dummy = ListNode(0)
dummy.next = head
pre = dummy
p = head
q = p
for i in range(n):
q = q.next
while q:
pre = pre.next
p = p.next
q = q.next
pre.next = p.next
return dummy.next
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pHead ListNode类
# @param k int整型
# @return ListNode类
#
class Solution:
def FindKthToTail(self , pHead , k ):
# write code here
if not pHead:
return None
fast, slow = pHead, pHead
while fast and k > 0:
fast = fast.next
k -= 1
if k > 0:
return None
while fast:
fast = fast.next
slow = slow.next
return slow