링크
뒤에서 n번째 노드 제거하기
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
pointerA = pointerB = head
length = 0
while pointerB:
length += 1
pointerB = pointerB.next
if length == n:
return head.next
for _ in range(length - n - 1):
pointerA = pointerA.next
pointerA.next = pointerA.next.next
return head
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
fast = slow = head
for _ in range(n):
fast = fast.next
if not fast:
return head.next
while fast.next:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return head