数据结构与算法每日一题——链表(203. 移除链表元素)

48 阅读1分钟

[203. 移除链表元素]

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function(head, val) {
    const temp = new ListNode(0,head)
    let cur = temp
    while(cur.next){
        if(cur.next.val === val){
            cur.next = cur.next.next
            continue
        }
        cur = cur.next
    }
    return temp.next
};