链表系列之 无序单链表去重

  算法专题导航页面


【算法专题 - 链表】

  1. 链表系列之 无序单链表去重
  2. 链表系列之 滑动窗口问题 – 所有生成窗口的最大值的集合
  3. 链表系列之 有环单链表判断

【题目】
  现有一个无序单链表,其中存在重复元素,实现一个算法来删除重复出现的元素,最终使得单链表中所有元素仅出现一次。

【其他限制】
  无

【图示】
  假设无序单链表为如下结构(蓝色):
在这里插入图片描述
【分析】
  针对本问题,最简单的思路也即两层循环,针对每一个元素查找所有与其值相等的元素,然后删除即可,因为存在嵌套循环,故时间复杂度为O(N2)。
在这里插入图片描述
  仔细思考,遍历查找类问题的一个行之有效的方法是合理使用哈希表。如此只需要遍历一边,遍历的过程中做好标识,即可以在遍历过程中发现重复元素,跳过重复元素即可。

【代码实现】

/* Node struct */
public Node {
    public int data;
    public Node next;
    public Node(int data) {
        this.data  = data;
    }
}
  1. 时间复杂度O(N2),空间复杂度O(1)
/*
 * DESC:
 * A function can remove the duplicted elememnts of a no sorted linklist
*/
public void removeDuplicatedElements_1(Node head) {
    Node cur = head;
    Node pre = NULL;
    Node next = NULL;
    while (NULL != cur) {
        pre = cur;
        next = cur.next;
        while (NULL != next) {
            if (cur.data == next.data) {
                pre.next = next.next;
                // Maybe you should free the resource used by pointer 'next' here
                // free(next);
            } else {
                pre = next;
            }
            next = next.next;
        }
        cur = cur.next;
    }
}
  1. 时间复杂度O(N),空间复杂度O(N)
public void removeDuplicatedElements_2(Node head) {
    if (NULL == head)
        return;
    HashSet<Integer> hash_set = new HashSet<Integer>();
    Node pre = head;
    Node cur = head.next;
    hash_set.add(head.data);
    while (NULL != cur) {
        if (hash_set.contains(cur.data)) {
            pre.next = cur.next;
        } else {
            hash_set.add(cur.data);
            pre = cur;
        }
        cur = cur.next;
    }
}
  • 11
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
无序单链表去重操作可以使用哈希表来实现。具体步骤如下: 1. 定义一个哈希表,用于记录链表中已经出现过的元素。 2. 遍历链表节点,对于每个节点,首先在哈希表中查找该节点的值是否存在。 3. 如果哈希表中已存在该值,则说明链表中已经有相同的元素,将该节点从链表中删除;如果哈希表中不存在该值,则将该值添加到哈希表中,继续遍历下一个节点。 4. 最终,遍历完整个链表后,链表中的重复元素都已经被删除。 具体实现的代码如下所示: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 struct ListNode { int val; struct ListNode* next; }; void removeDuplicates(struct ListNode* head) { if (head == NULL) { return; } // 定义哈希表 int hashSet[1000] = {0}; struct ListNode* pre = head; struct ListNode* cur = head->next; // 遍历链表节点 while (cur != NULL) { // 如果哈希表中已存在该值,则删除当前节点 if (hashSet[cur->val] != 0) { pre->next = cur->next; free(cur); cur = pre->next; } else { // 否则将该值添加到哈希表中 hashSet[cur->val] = 1; pre = cur; cur = cur->next; } } } // 创建一个链表节点 struct ListNode* createNode(int val) { struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode)); node->val = val; node->next = NULL; return node; } // 打印链表 void printList(struct ListNode* head) { struct ListNode* cur = head; while (cur != NULL) { printf("%d ", cur->val); cur = cur->next; } printf("\n"); } int main() { // 创建链表示例:1->2->3->2->4->3 struct ListNode* head = createNode(1); head->next = createNode(2); head->next->next = createNode(3); head->next->next->next = createNode(2); head->next->next->next->next = createNode(4); head->next->next->next->next->next = createNode(3); printf("原始链表:"); printList(head); removeDuplicates(head); printf("去重后的链表:"); printList(head); return 0; } ``` 以上代码就可以实现对无序单链表进行去重操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值