删除链表中重复的节点

删除链表中重复的结点

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

删除链表中重复的节点:

首先删除链表的节点我们必然需要至少两个指针,跑在前面的叫head,后面的叫post。首先把遍历整个链表的代码(不加删除节点)写出来:

private HashSet<Integer> set = new HashSet<>();
    public ListNode deleteDuplication(ListNode pHead) {
        ListNode head = pHead;
        ListNode post = pHead;
        while(head != null){
            set.add(head.val);
            head = head.next;
            post.next = head;
            post = post.next;
        }
        return pHead;
    }

然后遇到重复的节点只需要head = head.next,删除节点时只需要 post.next = head即可。所以代码的雏形就出来了:(注意,这里只是去掉多余的重复节点即1->2->3->4->5,而题中要求去掉所有重复节点1->2->5)。

    private HashSet<Integer> set = new HashSet<>();
    public ListNode deleteDuplication(ListNode pHead) {
        ListNode head = pHead;
        ListNode post = pHead;
        while(head != null){
            set.add(head.val);
            head = head.next;

            while(head != null && set.contains(head.val)){
                head = head.next;
            }
            post.next = head;
            post = post.next;
        }
        return pHead;
    }

所以我们需要两个set用来找到重复的节点结合。代码如下:

private HashSet<Integer> setAll = new HashSet<>();
private HashSet<Integer> set = new HashSet<>();

while(head != null){
    if(setAll.contains(head.val))
        set.add(head.val);
    setAll.add(head.val);
    head = head.next;
}

剩余的代码和前面一样,只不过由于第一个节点也有可能是重复节点,所以需要多一次过滤,从第一个不是重复的节点开始。完整的代码如下:

    private HashSet<Integer> setAll = new HashSet<>();
    private HashSet<Integer> set = new HashSet<>();
    public ListNode deleteDuplication(ListNode pHead) {
        ListNode head = pHead;
        ListNode post = pHead;

        while(head != null){
            if(setAll.contains(head.val))
                set.add(head.val);
            setAll.add(head.val);
            head = head.next;
        }

        head = pHead;
        while(head != null && set.contains(head.val)){
            head = head.next;
        }
        pHead = head;
        post = pHead;
        while(head != null){
            head = head.next;
            while(head != null && set.contains(head.val)){
                head = head.next;
            }
            post.next = head;
            post = post.next;
        }
        return pHead;
    }
全部评论

相关推荐

Java抽象带篮子:难蚌,点进图片上面就是我的大头😆
点赞 评论 收藏
分享
11-01 08:48
门头沟学院 C++
伤心的候选人在吵架:佬你不要的,能不能拿户口本证明过户给我。。球球了
点赞 评论 收藏
分享
蚂蚁 基架java (n+6)*16 签字费若干
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务