leetcode 203

struct ListNode* removeElements(struct ListNode* head, int val)
{
    struct ListNode * tmp = (struct  ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode * pre = tmp;
    pre->next = head;
    while(head != NULL)
    {
        if(head->val == val) {
            head= head->next;
            pre->next = head;
        }else{
            head = head->next;
            pre = pre->next;
        }
    }
    return tmp->next;
}

基本思路:

在删某个节点的时候需要修改该节点前面节点的next指针,所以需要有一个pre指针

另外需要有个节点来保存节点最初的位置 。

struct ListNode* removeElements(struct ListNode* head, int val)
{
    struct ListNode * tmp = (struct  ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode * pre = tmp;
    pre->next = head;
    while(head != NULL)
    {
        if(head->val == val) {
            head= head->next;
            pre->next = head;
        }else{
            head = head->next;
            pre = pre->next;
        }
    }
    return tmp->next;
}

时间: 2024-12-14 18:38:25

leetcode 203的相关文章

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

Java [Leetcode 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 解题思路: 链表操作. 代码如下: /** * Definition for singly-linked

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 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的元素结点 解题思路: 重点在于找到第一个非val的头结点,然后遍历链表,依次删除值为

Leetcode 203. Remove Linked List Elements JAVA语言

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 题意:删除链表中的节点 /**  * Definition for singly-linked list.  * 

Java for 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 解题思路: JAVA实现如下: public ListNode removeElements(ListNode h

LeetCode#203 Count Prime

Problem Definition: 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 Solution: 构造一个头节点 def removeElements(

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

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