LC.203. Remove Linked List Elements

230,82,83 是一类题

https://leetcode.com/problems/remove-linked-list-elements/description/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

time o(1) space o(1)

 1  public ListNode removeElements(ListNode head, int val) {
 2         //这里不要写 HEAD.NEXT == NULL 返回 HEAD, 因为会出现 [1], VAL=1 -> []
 3         if (head == null ) return head;
 4         ListNode dummy = new ListNode(0) ;
 5         ListNode curr = dummy ;
 6         dummy.next = head ;
 7         /*
 8         Given: 1 --> 2 --> 6 --> 6 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
 9         Return: 1 --> 2 --> 3 --> 4 --> 5
10         * */
11         while (curr != null && curr.next!= null){
12             if (curr.next.val == val){
13                 curr.next = curr.next.next ;
14             } else{
15                 curr = curr.next ;
16             }
17         }
18         return dummy.next ;
19     }

原文地址:https://www.cnblogs.com/davidnyc/p/8460734.html

时间: 2024-07-31 00:54:23

LC.203. Remove Linked List Elements的相关文章

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

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】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 题解: 这道题没什么好讲的,基础操作.需要注意的是链表的第一个node,因为没有前驱节点,所以该node需

[leedcode 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 list. * public class Lis

203. Remove Linked List Elements (List)

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 list. * struct ListNode

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