1.2从链表中移除重复项

Posted miao-study

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1.2从链表中移除重复项相关的知识,希望对你有一定的参考价值。

无序链表移除重复项

# -*-coding:utf-8-*-
class Node:
    def __init__(self, data=None, next=None):
        self.data = data
        self.next = next


def print_link(head):
    cur = head.next
    while cur.next != None:
        print(cur.data, end=' ')
        cur = cur.next
    print(cur.data)


def delete_rep(head):
    flag = set()
    pre = head
    cur = head.next
    while pre.next != None:
        if cur.data not in flag:
            flag.add(cur.data)
            pre = cur
            cur = cur.next
        else:
            pre.next = cur.next
            cur = cur.next
    print_link(head)


def con_link(n):
    nums = list(map(int, n.split(' ')))
    head = Node()
    cur = head
    for num in nums:
        node = Node(num)
        cur.next = node
        cur = node
    print_link(head)
    delete_rep(head)


if __name__ == '__main__':
    n = input("请输入链表串:")
    con_link(n)

有序链表移除重复项

# -*-coding:utf-8-*-
class Node:
    def __init__(self, data=None, next=None):
        self.data = data
        self.next = next


def print_link(head):
    cur = head.next
    while cur.next != None:
        print(cur.data, end=' ')
        cur = cur.next
    print(cur.data)


def delete_rep(head):
    pre = head
    cur = head.next
    while pre.next != None:
        if pre.data == cur.data:
            pre.next = cur.next
        else:
            pre = cur
        cur = cur.next
    print_link(head)

def con_link(n):
    nums = list(map(int, n.split(' ')))
    head = Node()
    cur = head
    for num in nums:
        node = Node(num)
        cur.next = node
        cur = node
    print_link(head)
    delete_rep(head)


if __name__ == '__main__':
    n = input(">>>:")
    con_link(n)

以上是关于1.2从链表中移除重复项的主要内容,如果未能解决你的问题,请参考以下文章

链表有序链表中移除重复项

LeetCode 82 Remove Duplicates from Sorted List II(从已排序链表中移除重复元素)(Linked List)(*)

279,对链表进行插入排序

147. 对链表进行插入排序

力扣——链表题 203.移除链表元素83.删除排序链表中重复元素82.删除排序链表中重复元素Ⅱ206.反转链表 876.链表的中间节点

LeetCode--147.对链表进行插入排序