首页 > 试题广场 >

删除链表的节点

[编程题]删除链表的节点
  • 热度指数:65532 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。返回删除后的链表的头节点。

1.此题对比原题有改动
2.题目保证链表中节点的值互不相同
3.该题只会输出返回的链表和结果做对比,所以若使用 C 或 C++ 语言,你不需要 free 或 delete 被删除的节点

数据范围:
0<=链表节点值<=10000
0<=链表长度<=10000
示例1

输入

{2,5,1,9},5

输出

{2,1,9}

说明

给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 2 -> 1 -> 9   
示例2

输入

{2,5,1,9},1

输出

{2,5,9}

说明

给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 2 -> 5 -> 9   

说明:本题目包含复杂数据结构ListNode,点此查看相关信息
struct ListNode* deleteNode(struct ListNode* head, int val ) 
{
    //val在头结点的情况,直接更新头结点指针
    if(head->val == val)
    {
        head = head->next;
        return head;
    }

    //val不在头结点的情况,定位val结点位置
    struct ListNode* pcur = head;//val结点
    struct ListNode* prev = head;//val结点的前驱指针
    while(pcur->val != val)
    {
        prev = pcur;
        pcur = pcur->next;
    }
    prev->next = pcur->next;

    return head;

}

编辑于 2024-03-28 10:51:48 回复(0)
struct ListNode* deleteNode(struct ListNode* head, int val ) {
    struct ListNode* head1=head;
    struct ListNode* prev=NULL;
    if(head->val==val)
    {
        return head->next;
    }
    while(head)
    {
        if(head->val==val&&prev!=NULL)
        {
            prev->next=head->next;
        }
        prev=head;
        head=head->next;
    }
    return head1;
}
编辑于 2024-03-23 22:03:53 回复(1)
/**
 * struct ListNode {
 *  int val;
 *  struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param head ListNode类
 * @param val int整型
 * @return ListNode类
 */
struct ListNode* deleteNode(struct ListNode* head, int val ) {
    // write code here
    struct ListNode* p=head;
    struct ListNode* pre=NULL;

    while(p!=NULL){
        if(p->val==val && pre!=NULL){
            struct ListNode* tem=p;
            pre->next=p->next;
            p=pre->next;
            free(tem);
        }else if(p->val==val && pre==NULL){
            struct ListNode* tem=p;
            head=p->next;
            p=head;
            free(tem);

        }
        pre=p;
        p=p->next;
    }
    return head;
}
发表于 2024-01-17 19:07:43 回复(0)
struct ListNode* deleteNode(struct ListNode* head, int val ) {
    //添加一个头结点指针,
    struct ListNode* res = (struct ListNode*)malloc(sizeof(struct ListNode));
    res->next = head;
    struct ListNode* pre = res; //前序节点
    struct ListNode* cur = head;//当前节点

    while (cur!=NULL) {
        //注意:要先进性判断,防止链表第一个值就是val的情况
        if(cur->val == val) {
            pre->next = cur->next;  //跳过,断开连接
            break;
         }
        pre = cur;
        cur = cur->next;//这俩节点就是在head链表上进行修剪
    }
    //返回了从头节点之后开始的链表。因为 res->next指向了真正链表的头部
    return res->next;
}
编辑于 2024-01-04 17:07:45 回复(0)
struct ListNode* deleteNode(struct ListNode* head, int val ) {
    // write code here
    struct ListNode* pr = head;
    struct ListNode* pf = head;
    while (pf != NULL) {
        if (pf->val == val) {
            if (pf == head) head = head->next;
            else pr->next = pf->next;
            return head;
        } else {
            pr = pf;
            pf = pf->next;
        }
    }
    return head;
}

发表于 2023-11-25 10:28:31 回复(0)
struct ListNode* deleteNode(struct ListNode* head, int val ) 
{
    struct ListNode *header = (struct ListNode* )malloc(sizeof(struct ListNode));
    header->next = head; 
    struct ListNode *cur,*p;
    cur = header;

    while(cur->next->val!=val)
        cur = cur->next;
    
    p = cur->next;
    cur->next = cur->next->next;
    free(p);

    return header->next;
}

发表于 2023-04-12 15:32:25 回复(0)
/**
 * struct ListNode {
 *  int val;
 *  struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @param val int整型 
 * @return ListNode类
 */
struct ListNode* deleteNode(struct ListNode* headint val ) 
{
    // write code here
    if(!head)
    {
        return NULL;
    }
    struct ListNode* p = head;
    struct ListNode* q = head;

    while(->next != NULL)
    {
        if(->val == val) //找到这个要删除的结点val之后跳出循环
        {
            break;
        }
        q = p;
        p = -> next;
    }

    if(p)
    {
        if(-> val == val && p == head)  //这个要删除的节点在开头
        {
            head = head -> next;
            -> next = NULL;
        }
        else if(-> val == val && -> next == NULL)  //这个要删除的节点在结尾
        {
            -> next = NULL;
        }
        else if(-> val == val && -> next != NULL && p != head)  //这个要删除的节点在中间
        {
            -> next = -> next;
            -> next = NULL;
        }
        free(p);
    }
    else if(!p)
    {
        printf("没有找到这个要删除的%d\n",val);
    }

    return head;
}
发表于 2022-09-30 16:55:26 回复(0)
struct ListNode* deleteNode(struct ListNode* head, int val ) {
    struct ListNode* L = (struct ListNode* )malloc(sizeof(struct ListNode));
    L->next = head;
    struct ListNode* p = head;
    struct ListNode* pre = L;
    while(p) {
        if(p->val == val) {
            p = p->next;
            pre->next = p;

        } else {
            p = p->next;
            pre = pre->next;
        }
    }
    return L->next;
}

发表于 2022-09-16 16:16:05 回复(0)
struct ListNode* deleteNode(struct ListNode* head, int val ) 
{
    // write code here
    if(head->val==val)//特殊情况,删除头节点
    {
        return head->next;
    }
    else//设置前后指针
    {
        struct ListNode* prev=head;
        struct ListNode* tail=head;
        while(tail->val!=val)
        {
            prev=tail;
            tail=tail->next;
        }
        prev->next=tail->next;
        return head;
    }
}

发表于 2022-04-08 20:09:24 回复(0)
struct ListNode* deleteNode(struct ListNode* head, int val ) {
    if(head == NULL) return head;
    if(head->val == val) return head->next;
    struct ListNode *p = head,*pn = head->next;
    while(pn){
        if(pn->val == val){
            p->next = pn->next;
            free(pn);
            break;
        }
        p = pn;
        pn = pn->next;
    }
    return head;
    // write code here
}

发表于 2022-03-19 18:17:50 回复(0)
struct ListNode* deleteNode(struct ListNode* head, int val ) {
    if(head==NULL){
        return NULL;
    }
    if(head->val == val){
        return head->next;
    }
    struct ListNode* p = head;
    while(p->next != NULL && p->next->val != val){
        p = p->next;
    }
    // 找完了,还没找到
    if(p->next == NULL){
        return head;
    }
    // 找到了,删除它
    struct ListNode* s = p->next;
    p->next = s->next;
    free(s);// 释放空间
    return head; 
}
发表于 2021-11-21 21:09:51 回复(0)
/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @param val int整型 
 * @return ListNode类
 */
struct ListNode* deleteNode(struct ListNode* head, int val ) {
    // write code here
    //特殊情况,删除的是头节点
    if(head->val==val)
        return head->next;
    //设置前后指针,指向头节点和下一个节点。
    struct ListNode* front = head;
    struct ListNode* behind = head->next;
    while(behind!=NULL && behind->val!=val)
    {
        front=behind;
        behind=behind->next;
    }
    front->next = behind->next;
    return head;
}

发表于 2021-11-17 14:12:23 回复(0)