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

题目描述

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

解题方法

顺序删除

主要思路:通过双重循环直接在链表上进行删除操作。外层循环用一个指针从第一个结点开始遍历整个链表,然后内层循环用另外一个指针遍历其余结点,将与外层循环遍历到的指针所指结点的数据域相同的结点删除。
python算法 链表 如何从无序链表中移除重复项_第1张图片

代码实现
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)

python算法 链表 如何从无序链表中移除重复项_第2张图片

用空间换时间

主要思路:建立一个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算法 链表 如何实现链表的逆序

你可能感兴趣的:(python程序员面试--算法,链表,python,数据结构)