Skip to content

Latest commit

 

History

History
28 lines (25 loc) · 642 Bytes

19.-remove-nth-node-from-end-of-list.md

File metadata and controls

28 lines (25 loc) · 642 Bytes

19. Remove Nth Node From End of List

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        ## 双指针同向
        if not head:
            return head
        root = ListNode(-1)
        root.next = head
        q = p = root
        while n:
            p = p.next
            n -= 1
        # print(p.val)
        while p.next :
            q = q.next
            p = p.next

        q.next = q.next.next
        return root.next