题目
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Follow up: Could you do this in one pass?
解题思路
使用两个指针,第一先移动n次,然后同时移动,找到倒数第n个节点,然后删除
代码
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
first = ListNode(0)
second = ListNode(0)
first.next = head
second.next = head
for i in range(n):
first = first.next
if first.next == None:
return head.next
while first.next != None:
first = first.next
second = second.next
second.next = second.next.next
return head