题解 | #删除链表中重复的结点#

删除链表中重复的结点

http://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef

/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function deleteDuplication(pHead)
{
    // write code here
    if(!pHead) {
        return null;
    }
    let arr = [];
    let tempRes = pHead;
    let res = pHead;
    while(tempRes) {
        arr.push(tempRes.val);
        tempRes = tempRes.next;
    }
    while(res.next) {
        if(arr.indexOf(res.next.val) !== arr.lastIndexOf(res.next.val)) {
            res.next = res.next.next;
        } else {
            res = res.next;
        }
    }
    if(arr.indexOf(pHead.val) !== arr.lastIndexOf(pHead.val)) {
        if(pHead.next) {
            return pHead.next;
        }
        return null;
    }
    return pHead;
}
module.exports = {
    deleteDuplication : deleteDuplication
};
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 18:00
点赞 评论 收藏
分享
06-11 13:34
门头沟学院 C++
offe从四面八方来:我真的没时间陪你闹了
点赞 评论 收藏
分享
05-07 17:58
门头沟学院 Java
wuwuwuoow:1.简历字体有些怪怪的,用啥写的? 2.Redis 一主二从为什么能解决双写一致性? 3.乐观锁指的是 SQL 层面的库存判断?比如 stock > 0。个人认为这种不算乐观锁,更像是乐观锁的思想,写 SQL 避免不了悲观锁的 4.奖项证书如果不是 ACM,说实话没什么必要写 5.逻辑过期时间为什么能解决缓存击穿问题?逻辑过期指的是什么 其实也没什么多大要改的。海投吧
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

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