Remove Linked List Elements

问题:

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

思路:

  简单的链表操作

我的代码:

public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null) return null;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        while(head != null)
        {
            if(head.val == val)
            {
                pre.next = head.next;
                head = head.next;
            }
            else
            {
                pre = pre.next;
                head = head.next;
            }
        }
        return dummy.next;
    }
}

时间: 2024-08-09 14:43:22

Remove Linked List Elements的相关文章

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][JavaScript]Remove Linked List Elements

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 https://leetcode.com/problems/r

leetcode_203题——Remove Linked List Elements(链表)

Remove Linked List Elements Total Accepted: 8053 Total Submissions: 29898My Submissions Question Solution Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val =

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

203. Remove Linked List Elements - LeetCode

Question 203.?Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 Java实现: public ListNode removeElements(ListNode head, int val) { ListNode cur = head; while (cur != null) { if (cur.next != null && cur.next.val ==

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 1 /** 2 * Definition for singly-linked list. 3 * struct Lis

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:Remove Linked List Elements

Problem: 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->5Solution: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * in

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 /**