# 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):
q = p = head
dis = 0
if head == None or (head.next==None and n==1):
return None
while dis != n+1 or q!= None:
if dis>n:
p=p.next
dis-=1
if q != None:
q = q.next
dis+=1
if q == None and dis < n+1:
return head.next
if p.next != None:
p.next = p.next.next
return head
LeetCode #19 Remove Nth Node From End of List
最新推荐文章于 2015-10-08 17:48:34 发布