19. Remove Nth Node From End of List
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
solution
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy = ListNode(next=head)
i = 0
slow = fast = dummy
while i < n:
fast = fast.next
i += 1
while fast.next is not None:
slow = slow.next
fast = fast.next
slow.next = slow.next.next
return dummy.next时间复杂度:O(n) 空间复杂度:O(1)
follow up
237. Delete Node in a Linked List
*708. Insert into a Sorted Circular Linked List
时间复杂度:O(n) 空间复杂度:O(1)
Last updated