题解 | #删除链表的倒数第n个节点#
删除链表的倒数第n个节点
http://www.nowcoder.com/practice/f95dcdafbde44b22a6d741baf71653f6
class Solution:
def removeNthFromEnd(self , head: ListNode, n: int) -> ListNode:
if not head: return None
mark,mark.next,node=ListNode(0),head,self.find_node(head, n)
curr=mark
while curr:
if curr.next==node:
curr.next=curr.next.next
break
curr=curr.next
return mark.next
def find_node(self,head,n):
left,right=head,head
for _ in range(n):
right=right.next
while right:
left=left.next
right=right.next
return left
题解-数据结构与算法 文章被收录于专栏
小菜鸟的题解