leetcode 203: Remove Linked List Elements

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

Remove Linked List Elements

Total Accepted: 2921 Total Submissions: 10427

Remove all elements from a linked list of integers that have value val.

Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

Credits:
Special thanks to for adding this problem and creating all test cases.

[思路]

没啥好说的, 两个指针, 一样的就删, 不一样就next.


/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode p = dummy;
        ListNode q = head;
        while(q!=null) {
            if(q.val == val) {
                p.next = q.next;
            } else {
                p = p.next;
            }
            q = q.next;
        }
        
        return dummy.next;
    }
}



LeetCode题目详解】 203. 移除链表元素707. 设计链表206. 反转链表 day3(补) 关键是初始化的地方,可能有的同学会不理解, 可以看到双指针法中初始化 cur = head,pre = NULL,在递归法中可以从如下代码看出初始化的逻辑也是一样的,只不过写法变了。使用这种方法移除了一个头结点,是不是发现,在单链表中移除头结点 和 移除其他节点的操作方式是不一样,其实在写代码的时候也会发现,需要单独写一段逻辑来处理移除头结点的情况。为什么要保存一下这个节点呢,因为接下来要改变 cur->next 的指向了,将cur->next 指向pre ,此时已经反转了第一个节点了。 阅读详情

相关推荐

刷题之Leetcode203题(超级详细)

这样移除了一个头结点,是不是发现,在单链表中移除头结点 和 移除其他节点的操作方式是不一样,其实在写代码的时候也会发现,需要单独写一段逻辑来处理移除头结点的情况。那么因为单链表的特殊性,只能指向下一个节点,刚刚删除的是链表的中第二个,和第四个节点,那么如果删除的是头结点又该怎么办呢?移除头结点和移除其他节点的操作是不一样的,因为链表的其他节点都是通过前一个节点来移除当前节点,而头结点没有前一个节点。来看一下,如何移除元素1 呢,还是熟悉的方式,然后从内存中删除元素1。依然还是在这个链表中,移除元素1。

qq_69748833的博客 1334

力扣(LeetCode)第203题 -- 移除链表元素

本文结合伪代码对力扣“203.移除链表元素”进行了详细的讲解。

@Peixian Liang 426

LeetCode203题—移除链表元素

LeetCode203

Green_756的博客 623

LeetCode203题 移除链表元素 C语言实现Java实现

题目描述 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。 示例 1: 输入:head = [1,2,6,3,4,5,6], val = 6 输出:[1,2,3,4,5] 示例 2: 输入:head = [], val = 1 输出:[] 示例 3: 输入:head = [7,7,7,7], val = 7 输出:[] 来源:力扣(LeetCode) 链接:https://leetcode-c.

weixin_44916945的博客 324

LeetCode203:Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 Credits: Special thanks

1948

LeetCode 203:Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 移除单链表中所有值为val的节点。 例如:给定1

judadeshu的博客 274

LeetCode:203. Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. Example: Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5 代码: /** * Defi...

nicolelili1的专栏 134

Leetcode 203:Remove Linked List Elements

题目描述 这是一道关于链表的题目,原题如下 Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head. 稍微做个说明:给定一个值val,删除链表中值等于该val的所有节点,返回新链表的head节点。对于链表的删除操作,我们需要知道该节点的上一个节点的信息和下一个节点的信息。但是给定

weixin_44607838的博客 191

leetcode[203]:Remove Linked List Elements

Remove Linked List Elements Remove all elements from a linked list of integers that have value val.Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5Credits: Spec

hahubaba的博客 216

Leetcode 203: Remove Linked List Elements

问题描述: Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head. 去掉单链表中=val的节点 思路: 因为是单链表,一般来讲重构链表操作需要用到两个指针,而且两个指针位置错开才有意义。比如说这道题,如果指针iter探察到了当下这个节点该删,这时候必须有一个该节点之前的指针用来定位

weixin_51401145的博客 130

leetcode: 203. Remove Linked List Elements

leetcode: 203. Remove Linked List Elements 原题链接 方法一:直接使用原来的链表来进行移除节点操作: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next

LSK的博客 177

LeetCode笔记:203. Remove Linked List Elements

删除链表中特定值的所有节点

Minecraft 816

[leetcode]: 203. Remove Linked List Elements

1.题目Remove all elements from a linked list of integers that have value val. 删除列表中所有的指定元素 Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 52.分析两种方式:迭代or递归3.代码迭代cl

沐沐余风的专栏 410

LeetCode: 203. Remove Linked List Elements

LeetCode: 203. Remove Linked List Elements 题目描述 Remove all elements from a linked list of integers that have value val. Example: Input: 1->2->6->3->4->5->6, val = 6 Output: 1-&g...

杨领well的专栏 207

leetcode:(203) remove linked list elements(java)

package LeetCode_LinkedList; /** * 题目: * Remove all elements from a linked list of integers that have value val. * Example: * Input: 1->2->6->3->4->5->6, va...

Sunshine_liang1的博客 194
上一篇: Leetcode 198: House Robber
下一篇: leetcode 202: Happy Number
xudli
博客等级 码龄18年 204粉丝 275原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值