您的位置:首页 > Web前端 > Node.js

LeetCode #19 Remove Nth Node From End of List

2015-07-23 11:57 429 查看
# 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: