码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 203. Remove Linked List Elements

时间:2019-10-09 09:43:37      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:new   bsp   discus   ber   turn   ret   val   java   function   

在实现JS版本之前我有实现过Java版本,很奇怪为什么discussion里面大多数JS版本的解法都没有用dummy node。

 1 /**
 2  * @param {ListNode} head
 3  * @param {number} val
 4  * @return {ListNode}
 5  */
 6 var removeElements = function(head, val) {
 7     if (head === null) return head;
 8     let dummy = new ListNode(0);
 9     dummy.next = head;
10     let cur = dummy;
11     while (cur.next != null) {
12         if (cur.next.val === val) {
13             cur.next = cur.next.next;
14         } else {
15             cur = cur.next;
16         }
17     }
18     return dummy.next;
19 };

 

[LeetCode] 203. Remove Linked List Elements

标签:new   bsp   discus   ber   turn   ret   val   java   function   

原文地址:https://www.cnblogs.com/aaronliu1991/p/11639415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!