python算法 链表 如何从无序链表中移除重复项

题目描述

给定一个没有排序的链表,去掉其重复项,并保留原顺序。
例如:1->2->2->3->4->3->4->5
去掉重复项后变成:1->2->3->4->5

解题方法

顺序删除

主要思路:通过双重循环直接在链表上进行删除操作。外层循环用一个指针从第一个结点开始遍历整个链表,然后内层循环用另外一个指针遍历其余结点,将与外层循环遍历到的指针所指结点的数据域相同的结点删除。
在这里插入图片描述

代码实现
class LNode:
    def __init__(self, x=None):
        self.data = x
        self.next = None
        
def InputData():
    head = LNode()
    point = head
    print("请输入数据,每输入一个结点数据就回车一次,直接回车就结束输入。")
    data = input()
    try:
        data = eval(data)
    except:
        pass
    while data!='':
        node = LNode()
        node.data = data
        node.next = None
        point.next = node
        point = node
        data = input()
        try:
            data = eval(data)
        except:
            pass
    return head

def Output(head):
    if head == None or head.next == None:
        return
    cur = head.next
    cnt = 0
    print("打印数据结果:")
    while cur:
        cnt += 1
        print(cur.data, end='\t')
        cur = cur.next
        if cnt % 10 == 0:
            print("\n")
    print("\n")
 
def removeDup(head):
    if head == None or head.next == None:
        return 
    outcur = head.next
    incur = None
    inpre = None
    while outcur is not None:
        incur = outcur.next
        inpre = outcur
        while incur is not None:
            if incur.data == outcur.data:
                inpre.next = incur.next
                incur = incur.next
            else:
                inpre = incur
                incur = incur.next
        outcur = outcur.next

head = InputData()
Output(head)
removeDup(head)
Output(head)

在这里插入图片描述

用空间换时间

主要思路:建立一个hashset(集合set),hashset记录已经遍历过的结点值。如果结点值在hashset中,删除该结点,否则就将该结点添加到hashset中。

代码实现
class LNode:
    def __init__(self, x=None):
        self.data = x
        self.next = None
        
def InputData():
    head = LNode()
    point = head
    print("请输入数据,每输入一个结点数据就回车一次,直接回车就结束输入。")
    data = input()
    try:
        data = eval(data)
    except:
        pass
    while data!='':
        node = LNode()
        node.data = data
        node.next = None
        point.next = node
        point = node
        data = input()
        try:
            data = eval(data)
        except:
            pass
    return head

def Output(head):
    if head == None or head.next == None:
        return
    cur = head.next
    cnt = 0
    print("打印数据结果:")
    while cur:
        cnt += 1
        print(cur.data, end='\t')
        cur = cur.next
        if cnt % 10 == 0:
            print("\n")
    print("\n")
 
def removeDup2(head):
    if head == None or head.next == None:
        return 
    
    nodepre = head
    nodecur = nodepre.next
    hashset = set()
    while nodecur is not None:
        if nodecur.data in hashset:
            nodepre.next = nodecur.next
            nodecur = nodecur.next
        else:
            hashset.add(nodecur.data)
            nodepre = nodecur
            nodecur = nodecur.next

head = InputData()
Output(head)
removeDup2(head)
Output(head)

上一节:python算法 链表 如何实现链表的逆序

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是强筱华哇!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值