LC.203. Remove Linked List Elements

Posted davidnyc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.

Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 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     }

 

以上是关于LC.203. Remove Linked List Elements的主要内容,如果未能解决你的问题,请参考以下文章

[LC]203题 Remove Linked List Elements (移除链表元素)(链表)

LC-203

203. Remove Linked List Elements

日常系列LeetCode《14·链表1》

力扣算法JS LC [203. 移除链表元素] LC [206. 反转链表]

[E模拟] lc203. 移除链表元素(链表+模拟)