其他分享
首页 > 其他分享> > Leetcode19. 删除链表的倒数第 N 个结点(中等)双指针

Leetcode19. 删除链表的倒数第 N 个结点(中等)双指针

作者:互联网

struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
        struct ListNode* dummyHead = (struct ListNode*)malloc(sizeof(struct ListNode));
        dummyHead->next = head;
        struct ListNode*slow=dummyHead,*fast=dummyHead;
        for(int i=0;i<n+1;i++){
            fast=fast->next;
        }
        while(fast!= NULL){
            slow=slow->next;
            fast=fast->next;
        }
        struct ListNode*temp=slow->next;
        slow->next=temp->next;
        free(temp);
        return dummyHead->next;
}

标签:dummyHead,ListNode,struct,fast,next,链表,slow,Leetcode19,倒数第
来源: https://blog.csdn.net/m0_48711099/article/details/122773004