单链表 -- 删除重复结点,重复的结点不保留

题目中说得很清楚:排序的链表,那么如果存在重复元素的话,一定是紧挨着的,要么重复的是两个要么是多个。

package com.Linked;

public class MySingleList {

    class Node {

        private int data;
        private Node next;

        private Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    private Node head;
    public MySingleListImpl() {
        this.head = null;
    }

    public Node searchIndex(int index) {

        if (index < 0 || index > getLength()) {
            throw new UnsupportedOperationException();
        }

        int i = 0;
        Node cur = this.head;
        while (i < index-1) {
            cur = cur.next;
            i++;
        }
        return cur;
    }

    public boolean addIndex(int index, int data) {

        if (index == 0) {
            addFirst(data);
            return true;
        }
        if (index == getLength()) {
            addLast(data);
            return true;
        }
        Node node = new Node(data);
        Node pre = searchIndex(index);
        node.next = pre.next;
        pre.next = node;
        return true;
    }

    public void display() {

        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void show(Node newHead) {

        Node cur = newHead;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //在一个排序的链表中,删除该链表中重复的结点,
    //重复的结点不保留,返回链表头指针
    public Node deleteDuplication() {

        //虚拟节点
        Node node = new Node(-1);
        //让tmpHead移动,node作为头指针是要被返回的
        Node tmpHead = node;
        Node cur = this.head;
        while (cur != null) {
            if (cur.next != null && cur.data == cur.next.data) {
                while (cur.next != null && cur.data == cur.next.data) {
                    cur = cur.next;
                }
                cur = cur.next;
                //cur指向的这个节点和前面的不同
                tmpHead.next = cur;
            }else {
                //确定是不重复的节点,串起来就行
                tmpHead.next = cur;
                tmpHead = cur;
                cur = cur.next;
            }
        }
        return node.next;
    }
}
package com..Linked;

public class Test {

    public static void main(String[] args) {

		MySingleList list = new MySingleList();
		list.addIndex(0, 1);
        list.addIndex(1, 1);
        list.addIndex(2, 1);
        list.addIndex(3, 3);
        list.addIndex(4, 3);
        list.addIndex(5, 4);
        list.addIndex(6, 5);
        list.addIndex(7, 5);
        list.addIndex(8, 7);
        list.addIndex(9, 9);
        list.addIndex(10, 9);
     
        System.out.print("原链表:");
        list.display();
        
        System.out.print("删除重复元素后的链表为:");
        MySingleListImpl.Node cur = list.deleteDuplication();
        list.show(cur);
    }
}

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值