剑指offer第五十六题删除链表中重复的节点

题目描述

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

方法一:

非递归的代码:
1. 首先添加一个头节点,以方便碰到第一个,第二个节点就相同的情况。(这个方法有个具体的名字,我之前见过,就是为了保留第一个节点)哨兵节点
2.设置 pre ,last 指针, pre指针指向当前确定不重复的那个节点,而last指针相当于工作指针,一直往后面搜索。

package test;
class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}
/*
非递归的代码:
1. 首先添加一个头节点,以方便碰到第一个,第二个节点就相同的情况
2.设置 pre ,last 指针, pre指针指向当前确定不重复的那个节点,而last指针相当于工作指针,一直往后面搜索。
*/
class Solution {
    public ListNode deleteDuplication(ListNode pHead){
        if (pHead==null || pHead.next==null){
            return pHead;//这里返回的是第一个节点,而不是null,因为有可能就一个节点
        }
        ListNode Head = new ListNode(0);
        Head.next = pHead;
        ListNode pre  = Head;//这里初始化的时候pre是指向空的
        ListNode last = Head.next;//而last指向第一个
        while (last!=null){
            if(last.next!=null && last.val == last.next.val){//不要忘了last.next!=null
                // 找到最后的一个相同节点
                while (last.next!=null && last.val == last.next.val){
                    last = last.next;
                }
                pre.next = last.next;//删除所有重复节点
                last = last.next;
            }else{//不相等的情况就直接往下走呗
                pre = pre.next;
                last = last.next;
            }
        }
        return Head.next;
    }
}
public class Main{
    public static void main(String[] args) {
        ListNode p1 = new ListNode(1);
        ListNode p2 = new ListNode(2);
        ListNode p3 = new ListNode(3);
        ListNode p4 = new ListNode(3);
        ListNode p5 = new ListNode(4);

        p1.next = p2;
        p2.next = p3;
        p3.next = p4;
        p4.next = p5;

        Solution p = new Solution();
        ListNode b = p.deleteDuplication(p1);
        while (b != null){
            System.out.println("结果:" + b.val);
            b = b.next;
        }
    }
}

方法二:递归版

package test;
class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}
class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        if (pHead == null || pHead.next == null) { // 只有0个或1个结点,则返回
            return pHead;
        }
        if (pHead.val == pHead.next.val) { // 当前结点是重复结点
            ListNode pNode = pHead.next;
            while (pNode != null && pNode.val == pHead.val) {
                // 跳过值与当前结点相同的全部结点,找到第一个与当前结点不同的结点
                pNode = pNode.next;
            }
            return deleteDuplication(pNode); // 从第一个与当前结点不同的结点开始递归
        } else { // 当前结点不是重复结点
            pHead.next = deleteDuplication(pHead.next); // 保留当前结点,从下一个结点开始递归
            return pHead;
        }
    }
}

public class Main{
    public static void main(String[] args) {
        ListNode p1 = new ListNode(1);
        ListNode p2 = new ListNode(2);
        ListNode p3 = new ListNode(3);
        ListNode p4 = new ListNode(3);
        ListNode p5 = new ListNode(4);

        p1.next = p2;
        p2.next = p3;
        p3.next = p4;
        p4.next = p5;

        Solution p = new Solution();
        ListNode b = p.deleteDuplication(p1);
        while (b != null){
            System.out.println("结果:" + b.val);
            b = b.next;
        }
    }
}

方法三:使用HashSet

package test;

import java.util.HashSet;

class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}
class Solution {
    public ListNode deleteDuplication(ListNode pHead){
        if(pHead == null){
            return  null;
        }
        // 先找出相同结点,存入 set
        HashSet<Integer> set = new HashSet<>();
        ListNode pre = pHead;
        ListNode cur = pHead.next;
        while(cur != null){
            if(cur.val == pre.val){
                set.add(cur.val);
            }
            pre = cur;
            cur = cur.next;
        }
        // 再根据相同节点删除
        // 先删头部
        while(pHead != null && set.contains(pHead.val)){
            pHead = pHead.next;
        }
        if(pHead == null){
            return null;
        }
        // 再删中间结点
        pre = pHead;
        cur = pHead.next;
        while(cur != null){
            if(set.contains(cur.val)){
                pre.next = cur.next;
                cur = cur.next;
            }else{
                pre = cur;
                cur = cur.next;
            }
        }
        return pHead;
    }
}

public class Main{
    public static void main(String[] args) {
        ListNode p1 = new ListNode(1);
        ListNode p2 = new ListNode(2);
        ListNode p3 = new ListNode(3);
        ListNode p4 = new ListNode(3);
        ListNode p5 = new ListNode(4);

        p1.next = p2;
        p2.next = p3;
        p3.next = p4;
        p4.next = p5;

        Solution p = new Solution();
        ListNode b = p.deleteDuplication(p1);
        while (b != null){
            System.out.println("结果:" + b.val);
            b = b.next;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值