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), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* cur = NULL;
        while(head && head->val == val){
            cur = head;
            head = head->next;
            delete cur;
        }

        cur = head;
        ListNode* nxt = NULL;
        if(head){
            nxt = head->next;
        }
        while(nxt){
            // cout<<nxt->val<<endl;
            if(nxt->val == val){
                cur->next = nxt->next;
                delete nxt;
                nxt = cur->next;
            }else{
                cur = nxt;
                nxt = nxt->next;
            }
        }
        return head;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10566059.html

时间: 2024-07-28 17:44:17

leetcode 203. 移除链表元素(Remove Linked List Elements)的相关文章

[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 203. 移除链表元素

题目链接:https://leetcode-cn.com/problems/remove-linked-list-elements/ 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5

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

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

203. 移除链表中的元素 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 题意:移除链表中指定val的元素 注意:考虑删除节点在尾部,以及连续删除两个相邻节点的情况 /** * Definit

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

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

[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

LeetCode Remove Linked List Elements 删除链表元素

题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 1

leetCode 203. Remove Linked List Elements 链表

203. 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 题目大意: 删除链表中全部的目标元素. 代码如下:

leetcode Remove Linked List Elements 203

Remove Linked List Elements Total Accepted: 11291 Total Submissions: 42824 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