题解 | #删除链表的节点#

删除链表的节点

https://www.nowcoder.com/practice/f9f78ca89ad643c99701a7142bd59f5d

/*class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @param val int整型 
 * @return ListNode类
 */
export function deleteNode(head: ListNode, val: number): ListNode {
    // write code here
    const virtualNode = new ListNode(-1)
    virtualNode.next = head
    let cur = virtualNode
    while(cur.next) {
        if(cur.next.val === val) {
            cur.next = cur.next.next
        } else {
            cur = cur.next
        }
    }
    
    return virtualNode.next
}

全部评论

相关推荐

LuvSran:是人我吃。老师就是学校呆久了,就业方面啥都不懂,还自以为是为了我们就业好。我学校就一破双非,计科入行率10%都没有,某老师还天天点名,说是出勤率抬头率前排率高了,华为什么的大厂就会来,我们就是不好好上课才没有厂来招。太搞笑了
点赞 评论 收藏
分享
牛客21331815...:像我一投就pass,根本不用焦虑泡池子
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务