`

203. Remove Linked List Elements

阅读更多

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实现

/**
 * 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 helper = new ListNode(0);
        helper.next = head;
        ListNode p = helper;
        while(p.next != null){
            if(p.next.val == val){
                ListNode next = p.next;
                p.next = next.next;
            }else{
                p = p.next;
            }
        }
        return helper.next;

        
    }
}

 

 

分享到:
评论

相关推荐

    python-leetcode题解之203-Remove-Linked-List-Elements.py

    python python_leetcode题解之203_Remove_Linked_List_Elements.py

    leetcode2sumc-LeetCode:LeetCode的一些题目

    leetcode 2 sum c LeetCode 帮助文档 帮助文档存放在Help文件夹下。 文件名 文件描述 ...complexitypython.txt Python的一些常规操作的...Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List

    leetcod 203.移除链表元素:

    (题目来源:https://leetcode.cn/problems/remove-linked-list-elements/description/) 解题思路有两种:(一)要移除链表中值为val的节点,我们肯定是要将链表遍历一遍的,关键是我们在遍历中如何操作是一个问题...

    leetcode跳跃-LeetCode:力扣刷题70道!

    203 移除链表元素 | Remove Linked List Elements 力扣 206 反转链表 | Reverse Linked List 队列 Queue 力扣 933 最近的请求次数 | Number of Recent Calls 力扣 225 用队列实现栈 | Implement Stack Using Queue ...

    双向链表 链表(C++编写)

    without header elements. (Because only one of the pointers in each header element is used, we could in fact combine them into a single header element without sacrificing(牺牲) this simplicity. ...

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    Leetcode部分试题解析

    27. **Remove Linked List Elements**:移除链表中的特定元素。可以使用迭代方法,同时保持指向下一个非目标节点的引用。 28. **Remove Element**:从数组中移除特定元素。双指针法可以有效地完成这个任务。 29. *...

    List实现 数据结构 模板

    class DoublyLinkedList { private: Node* head; public: DoublyLinkedList(); ~DoublyLinkedList(); // 添加其他成员函数,如:add、remove、get、set等 }; ``` 以上三种实现各有优缺点。数组实现适合于需要...

    队列实现火车厢重排的算法及代码(个人创作)

    // Node structure for linked list struct Node { int data; Node* next; }; // Linked Queue class class LinkQueue { private: Node* first, *rear; public: LinkQueue(); // Constructor ~LinkQueue(); /...

Global site tag (gtag.js) - Google Analytics