【 leetcode Top 100】19. 删除链表的倒数第 N 个结点

这篇博客介绍了如何高效地从单链表中删除指定位置的节点,包括两种方法:两次遍历和双指针。在两次遍历的方法中,首先获取总长度,然后删除目标节点;双指针法则是一次遍历,通过一个附加节点辅助操作,尤其在处理删除头节点的情况时更为简洁。这两种方法都避免了不必要的遍历,提高了效率。
摘要由CSDN通过智能技术生成

题目:

在这里插入图片描述

思想:

  1. 两次遍历:第一次遍历得到总长度,第二次遍历删除节点
  2. 递归:递归到链表最后,等回退的时候,删除节点
  3. 双指针:一次遍历,O(2n),推荐

注意:

本题中需要注意的点,就是一个删除的节点是第一个节点的时候,需要额外判断一下。

递归:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    void dfs(ListNode* &node,int& i,ListNode* pre,ListNode* &next,int n){
        if(node == nullptr){
            i=0;
            next = NULL;
            return ; 
        }
        dfs(node->next,i,node,next,n);
        i++;
        if(i==n){
            if(pre == nullptr){
                node = node->next;
            }else{
                pre->next = next;
            }
        }
        next = node;
        return ;
    }
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(!head)return nullptr;
        int i;
        ListNode* next;
        dfs(head,i,NULL,next,n);
        return head;
    }
};

双指针:

其实在注意到,题目中给的链表的构造函数的时候,就应该想到需要一个附加节点,也就是头节点,然后就能够想到需要双指针做法。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummy = new ListNode(0,head);
        ListNode *first = head,*second = dummy;
        for(int i=0;i<n-1;i++){
            first = first -> next;
        }
        while(first -> next !=NULL){
            second = second->next;
            first = first -> next;
        }
        if(second == dummy){
            head = head ->next;
        }else{
            second->next = second ->next ->next;
        }
        
        return head;
    }
};

当然对于删除head的做法,还有更好的:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummy = new ListNode(0,head);
        ListNode *first = head,*second = dummy;
        for(int i=0;i<n-1;i++){
            first = first -> next;
        }
        while(first -> next !=NULL){
            second = second->next;
            first = first -> next;
        }
        second->next = second ->next ->next;
        return dummy -> next;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值