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

删除链表中重复的结点

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
};
全部评论

相关推荐

评论
3
收藏
分享
牛客网
牛客企业服务