/**
* @author chencc
* @Description 删除单向链表中的给定值
* @Date 2022/3/4 10:57
*/
public class DeleteGivenValue {
public static class Node {
public int value;
public Node next;
}
public static Node deleteValue(Node head, int num) {
//head 来到第一个不需要删除的节点
while (head != null) {
if (head.value != num) {
break;
}
head = head.next;
}
//创建两个Node节点,来到head位置
Node pre = head;
Node cur = head;
while (cur != null) {
if (cur.value == num) {
pre.next = cur.next;
} else {
pre = cur;
}
//cur 跳下一位
cur = cur.next;
}
return head;
}
}
删除单向链表中的给定值
最新推荐文章于 2025-12-15 09:30:09 发布
这篇博客介绍了如何在Java中删除单向链表中的给定值。通过创建两个指针,逐步遍历链表并删除匹配到目标值的节点,最终返回新的链表头节点。这种方法有效地实现了链表节点的删除操作。
513

被折叠的 条评论
为什么被折叠?



