题解 | #删除链表的倒数第n个节点#
删除链表的倒数第n个节点
http://www.nowcoder.com/practice/f95dcdafbde44b22a6d741baf71653f6
# 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
L, node = 0, head
while node:
L += 1
node = node.next
dummy = ListNode(None)
dummy.next = head
cur = dummy
for i in range(L-n):
cur = cur.next
cur.next = cur.next.next
return dummy.next