码迷,mamicode.com
首页 > 其他好文 > 详细

1.2从链表中移除重复项

时间:2019-09-04 13:13:36      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:__init__   data   name   输入   ==   head   ini   map   sel   

无序链表移除重复项

# -*-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从链表中移除重复项

标签:__init__   data   name   输入   ==   head   ini   map   sel   

原文地址:https://www.cnblogs.com/miao-study/p/11458421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!