Leetcode deletes the Nth node from the bottom of the linked list c++

Delete the Nth node from the bottom of the linked list

Given a linked list, delete the nth node from the bottom of the linked list, and return the head node of the linked list.

Example:

给定一个链表: 1->2->3->4->5, 和 n = 2.
当删除了倒数第二个节点后,链表变为 1->2->3->5.

Description:

The given n guarantees are valid.

Advanced:

Can you try to use a scan to achieve it?


solution:

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        int count=0;//存放链表长度
        ListNode *p=head;
        while(p)//计算链表长度
        {
            count++;
            p=p->next;
        }
        p=head;
        if(count==1) return {};//链表只有一个元素
        else if(count-n==0) return head->next;//要删除的元素是第一个
        else 
        {
            for(int i=1;i<count-n;i++)
            {
                p=p->next;
            }
            p->next=p->next->next;
        }
        return head;
    }
};

Advanced: Dual pointer

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *h=new ListNode(0);
        h->next=head;
        ListNode *p=h;
        ListNode *k=h;
        if(head->next==NULL) return {};
        for(int i=0;i<=n;i++)
        {
            k=k->next;
        }
        while(k)
        {
            p=p->next;
            k=k->next;
        }
        p->next=p->next->next;
        return h->next;
    }
};

Guess you like

Origin blog.csdn.net/weixin_39139505/article/details/90273900