删除l链表中倒数第n值
这里用了双指针的巧妙运用就是,第一个指针先运动n+1个距离然后使得连两个指针之间的距离为n,然后一起运动使这样的距离保持到末尾这样就找到了那个第n个值
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
a = head
b = head
for i in range(n):
if a.next:
a = a.next
else:
return head.next
while a.next:
a = a.next
b = b.next
b.next = b.next.next
return head