leetcode203. 移除链表元素

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val) {
    while(head && head->val == val)
    {
        struct ListNode* tmp = head;
        head = head->next;
        free(tmp);
    }
    if(head == NULL) return head;
    struct ListNode* cur = head;
    while(cur->next)
    {
        if(cur->next->val == val)
        {
            struct ListNode* tmp = cur->next;
            cur->next = cur->next->next;
            free(tmp);
        }
        else
        {
            cur = cur->next;
        }
    }
    return head;
}

原文地址:https://www.cnblogs.com/xinfenglee/p/10074295.html

时间: 2024-08-30 14:52:10

leetcode203. 移除链表元素的相关文章

[Swift]LeetCode203. 移除链表元素 | 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 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->

[LeetCode] 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 to @mithmatt for adding this probl

3. 无重复字符的最长子串 141. 环形链表 171. Excel表列序号 203. 移除链表元素

3. 无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3.示例 2: 输入: "bbbbb"输出: 1解释: 因为无重复字符的最长子串是 "b",所以其长度为 1.示例 3: 输入: "pwwkew"输出: 3解释: 因为无重复字符的最长子串是 "

LeetCode 203 Remove Linked List Elements(移除链表元素)(*)

翻译 从一个链表中移除所有值为val的元素. 例如 给定:1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 返回:1 --> 2 --> 3 --> 4 --> 5 原文 Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 -

力扣(LeetCode)移除链表元素 个人题解

删除链表中等于给定值 val 的所有节点. 这题粗看并不困难,链表的特性让移除元素特别轻松,只用遇到和val相同的就跳过,将指针指向下一个,以此类推. 但是,一个比较麻烦的问题是,当链表所有元素都和val相同时,如果直接使用参数给的head,则返回的一定会保留第一位的节点,而题意是要返回空值. 对上述情况使用特判又会与"第一个节点的值和val不同,第二个节点之后和val值相同"相矛盾. 所以想到的思路是,新建一个节点,将这个节点接在head的最前面,保证第一个节点无意义,这样,哪怕遇到

[LC]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 ②中文题目 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val =

LeetCode OJ :Remove Linked List Elements (移除链表元素)

Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 ps:这一题感觉没什么技巧可言,选取一个头指针,一个当前指针,一个前向指针.简单的链表操作而已,代码如下: 1 /**

移除链表元素

删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5 /** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */clas

leetcode 203. 移除链表元素(Remove Linked List Elements)

目录 题目描述: 示例: 解法: 题目描述: 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5 解法: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), nex