首页 > 其他 > 详细

203. Remove Linked List Elements

时间:2019-06-20 14:13:39      阅读:89      评论:0      收藏:0      [点我收藏+]
/***
 * 203. Remove Linked List Elements
 * https://leetcode.com/problems/remove-linked-list-elements/description/
 *
 * Remove all elements from a linked list of integers that have value val.
 */
class ListNode(var `val`: Int) {
    var next: ListNode? = null
}

class Solution {
    fun removeElements(head: ListNode?, value: Int): ListNode? {
        //help by 2 pointer: dummy, pre
       val dummy = ListNode(-1)//fake head
        var pre = dummy
        dummy.next = head
        while (pre.next!=null) {
            if (pre.next!!.`val` == value) {
                pre.next = pre.next!!.next
            } else {
                pre = pre.next!!
            }
        }
        return dummy.next
    }

}

 

203. Remove Linked List Elements

原文:https://www.cnblogs.com/johnnyzhao/p/11058140.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!