首页 > 试题广场 >

删除链表的节点

[编程题]删除链表的节点
  • 热度指数:75165 时间限制: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,点此查看相关信息
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @param val int整型
     * @return ListNode类
     */
    public ListNode deleteNode (ListNode head, int val) {
        // write code here
        //确保知道要删除的结点的前一个结点
        ListNode p = head;
        ListNode pHead = p;
        while(head != null){
            if(val == head.val){//找到要删除的结点
                if(val == p.val){//此节点为头结点
                    pHead = head.next;
                    break;
                }
                //此节点不是头节点,将前一个结点的next指针指向下一个结点
                p.next = head.next;
            }
            if(p != head){//确保p始终为head前一个结点
                p = p.next;
            }
            head = head.next;
        }
        return pHead;
    }
}
发表于 2025-04-19 14:13:09 回复(0)
public ListNode deleteNode (ListNode head, int val) {
        // write code here
        ListNode temp = new ListNode(0);
        temp = head;
        // find the node for deleting
        if (head.val != val){
            while(head.next.val != val){
            head = head.next;
            }
            // delete the node
            head.next = head.next.next;
        } else{
            // delete the node
            temp = head.next;
        }
        return temp;
    }
}
发表于 2025-04-17 15:29:50 回复(0)
简单,1.遍历结点2.查到该值就删除跳出循环
双指针遍历连接或单指针直接遍历连接
发表于 2025-03-04 15:36:39 回复(0)
import java.util.*;

/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/

public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param val int整型
* @return ListNode类
*/
新建一个链表复制已有的链表,遇到需要删除的节点时,跳过该节点,返回新的节点开始就可以了

public ListNode deleteNode (ListNode head, int val) {
// write code here
ListNode newNodes=new ListNode(-1);
ListNode cur=newNodes;
while(head!=null){
if(head.val != val){
cur.next=new ListNode(head.val);
cur=cur.next;
}
head=head.next;
}
return newNodes.next;
}
}
发表于 2023-11-27 12:03:00 回复(0)
Doug Lea写法版,在JUC中发现的
public ListNode deleteNode (ListNode head, int val) {
    ListNode preNode = null;
    ListNode curNode = head;
    while (curNode != null) {
        ListNode next = curNode.next;
        if (curNode.val == val) {
            curNode.next = null;
            if (preNode == null) {
                head = next;
            } else
                preNode.next = next;
            if (next == null)
                break;
        } else
            preNode = curNode;
        curNode = next;
    }
    return head;
}


发表于 2023-05-07 23:49:47 回复(0)
public class Solution {
    public ListNode deleteNode (ListNode head, int val) {
        ListNode p = head;
        ListNode q = p.next;
        if (head.val == val) return head.next;
        while (q.val != val) {
            p = q;
            q = q.next;
        }
        p.next = q.next;
        return head;
    }
}

发表于 2023-03-07 19:35:59 回复(0)
public ListNode deleteNode (ListNode head, int val) {
    // write code here
    if(head.val != val){
        head.next = deleteNode(head.next, val);
    }
    if(head.val == val){
        head = head.next;
    }
    return head;
}
发表于 2022-12-26 00:51:33 回复(0)
import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @param val int整型
     * @return ListNode类
     */
    public ListNode deleteNode (ListNode headint val) {
        // write code here
        ListNode node = head;
        if(head.val==val)return head.next;
        while (node.next != null) {
            if (node.next.val == val) {
                ListNode temp = node.next.next;
                node.next = temp;
                break;
            }
            node = node.next;

        }
        return head;
    }
}
发表于 2022-11-03 16:48:51 回复(0)
public class Solution {

    public ListNode deleteNode (ListNode head, int val) {
        // write code here
        ListNode temp = head;
       if(head.val == val){
           return head.next;
       }
        while(head.next!= null){
            if(head.next.val == val){
                head.next = head.next.next;
            }else{
                head = head.next;
            }
        }
        return temp;
    }
}


发表于 2022-10-24 23:53:00 回复(0)
//递归
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @param val int整型 
     * @return ListNode类
     */
    public ListNode deleteNode (ListNode headint val) {
        // write code here
        if(head==null){
            return head;
        }
        head.next=deleteNode(head.next,val);
        return head.val==val?head.next:head;
    }
}
发表于 2022-10-13 11:04:47 回复(0)
import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @param val int整型 
     * @return ListNode类
     */
    public ListNode deleteNode (ListNode head, int val) {
        if(head==null) return null;
        if(head.val==val) return head.next;

        ListNode point = head;
        ListNode next = head.next;
        while(true){
            if(next!=null){
                if(next.val==val){
                    point.next = next.next;
                    return head;
                }else {
                    point = next;
                    next = next.next;
                }
            }else {
                return null;
            }
        }
    }
}

发表于 2022-09-27 10:42:36 回复(0)
import java.util.*;

public class Solution {
    public ListNode deleteNode (ListNode head, int val) {
        if(head.val==val){//如果要删除第一个结点
            ListNode second=head.next;
            head.next=null;
            return second;
        }
        ListNode pointNode=head;
        boolean found=false;
        while(true){
            if(pointNode.next==null){
                break;
            }
            if(pointNode.next.val==val){
                found=true;
                break;
            }
            pointNode=pointNode.next;
        }
        if(found){
            pointNode.next=pointNode.next.next;
        }

        return head;
    }
}

发表于 2022-09-24 00:43:58 回复(0)
public ListNode deleteNode (ListNode head, int val) {
        // write code here
        if(head == null) return null;
        ListNode cur = head;
        ListNode pre = null;
        if(head.val == val) return head.next;
        while(cur != null) {
            if(cur.val == val) {
                pre.next = cur.next;
            }
            pre = cur;
            cur = cur.next;
            
        }
        return head;
    }

发表于 2022-09-03 09:44:43 回复(0)
import java.util.*;
public class Solution {
    public ListNode deleteNode (ListNode head, int val) {
        //加入一个头节点
        ListNode res = new ListNode(0);
        res.next = head;
        //前序节点
        ListNode pre = res;
        //当前节点
        ListNode cur = head;
        //遍历链表
        while(cur != null){
            if(cur.val == val){
                pre.next = cur.next;
                break;
            }  
            pre = cur;
            cur = cur.next;
        }
        //返回去掉头节点
        return res.next;
    }
}

发表于 2022-08-31 16:33:35 回复(0)
 public ListNode deleteNode (ListNode head, int val) {
        // write code here
        ListNode dummpy = new ListNode(-1);
        dummpy.next = head;
        ListNode p = dummpy;
        ListNode cur = head;
        while (cur.val != val) {
            p = cur;
            cur = cur.next;
        }
        p.next = p.next.next;
        return dummpy.next;
    }

发表于 2022-08-13 17:27:27 回复(0)