LeetCode | 19. Delete the Nth node from the bottom of the linked list

LeetCode | 19. Delete the Nth node from the bottom of the linked list

OJ link

Insert image description here

Idea:

  • Define the virtual head nodedummy and initialize it to point tohead
  • Then define the fast and slow pointers
  • Let the fast pointer go n steps first
  • then go together
  • Finally delete the nth node from the last
  • Then release the virtual nodedummy
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    
    
    //定义虚拟头节点dummy 并初始化使其指向head
    struct ListNode* dummy = malloc(sizeof(struct ListNode));
    dummy->val = 0;
    dummy->next = head;
    //定义 fast slow 双指针
    struct ListNode* fast = head;
    struct ListNode* slow = dummy;

    for (int i = 0; i < n; ++i) {
    
    
        fast = fast->next;
    }
    while (fast) {
    
    
        fast = fast->next;
        slow = slow->next;
    }
    slow->next = slow->next->next;//删除倒数第n个节点
    head = dummy->next;
    free(dummy);//删除虚拟节点dummy
    return head;
}

おすすめ

転載: blog.csdn.net/2201_76004325/article/details/134515846