cci-Q2.1 未排序链表去重

本文介绍了如何通过哈希表和循环遍历链表的方法来解决从未排序链表中移除重复项的问题,并提供了一个不使用临时缓存的解决方案。实现了两个版本的算法,一个使用哈希表进行缓存,另一个则完全避免使用缓存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:

Write code to remove duplicates from an unsorted linked list.

FOLLOW UP

How would you solve this problem if a temporary buffer is not allowed?

译文:

从一个未排序的链表中移除重复的项

进一步地,

如果不允许使用临时的缓存,你如何解决这个问题?

首先定义一个node类

public class LinkedListNode {

    int data;
    LinkedListNode next;

    public LinkedListNode(int data) {
        this.data = data;
        this.next = null;
    }
}


1,允许使用临时的缓存,使用一个哈希表,如果遇到没有遇到过的元素写入哈希表,如果遇到的是重复的,删除。

public static void main(String args[]) {
        LinkedListNode head = null;
        LinkedListNode tmp = null;
        int a[] = {1, 2, 3, 3, 2, 1, 4};
        for (int i = 0; i < a.length; i++) {
            LinkedListNode next = new LinkedListNode(a[i]);
            if (i == 0) {
                head = tmp = next;
                continue;
            }
            tmp.next = next;
            tmp = next;
        }

        // Print the list(before) 
        tmp = head;
        while (tmp != null) {
            System.out.println(tmp.data);
            tmp = tmp.next;
        };

        delDup1(head);

        // Print the list(after) 

        while (head != null) {
            System.out.println(head.data);
            head = head.next;
        };
    }

    public static void delDup1(LinkedListNode head) {
        LinkedListNode n = head;
        Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
        LinkedListNode previous = null;
        while (n != null) {
            if (table.containsKey(new Integer(n.data))) {
                previous.next = n.next;
            } else {
                table.put(new Integer(n.data), new Integer(1));
                previous = n;
            }
            n = n.next;
        }
    }


2,不允许使用临时的缓存


 public static void main(String args[]) {
        LinkedListNode head = null;
        LinkedListNode tmp = null;
        int a[] = {1, 2, 3, 3, 2, 1, 4};
        for (int i = 0; i < a.length; i++) {
            LinkedListNode next = new LinkedListNode(a[i]);
            if (i == 0) {
                head = tmp = next;
                continue;
            }
            tmp.next = next;
            tmp = next;
        }

        // Print the list(before) 
        tmp = head;
        while (tmp != null) {
            System.out.println(tmp.data);
            tmp = tmp.next;
        };

        delDup(head);

        // Print the list(after) 

        while (head != null) {
            System.out.println(head.data);
            head = head.next;
        };
    }

    public static void delDup(LinkedListNode head) {
        if (head == null) {
            return;
        }

        LinkedListNode runner = head;
        LinkedListNode runnernext = head;
        LinkedListNode current = head;

        while (current != null) {
            runner = current;
            runnernext = current.next;
            int currentData = current.data;
            while (runnernext != null) {
                if (runnernext.data == currentData) {
                    runner.next = runnernext.next;
                    runnernext = runner.next;
                } else {
                    runner = runnernext;
                    runnernext = runner.next;
                }
            }
            current = current.next;
        }
    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值