leetcode刷题九

Posted hhh江月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode刷题九相关的知识,希望对你有一定的参考价值。

leetcode刷题九

题目叙述

给定一个已排序的链表的头 head , 删除原始链表中所有重复数字的节点,只留下不同的数字 。返回 已排序的链表 。

题目解析

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        head0 = head
        out = []
        record = None
        if head == None:
            return head
        while head.next != None:
            if head.val != head.next.val and head.val != record:
                out.append(head.val)
            else:
                record = head.val
            head = head.next
        if head.val != record:
            out.append(head.val)
        # print(out)

        head = head0
        if len(out) == 0:
            return None
        while head.val != out[0]:
            head = head.next
        head0 = head
        while head.next != None:
            if head.next.val in out:
                head = head.next
            else:
                head.next = head.next.next
        return head0
        

以上是关于leetcode刷题九的主要内容,如果未能解决你的问题,请参考以下文章

剑指Offer面试题九度OJ1384:二维数组中的查找

leetcode刷题总目录

Leetcode刷题笔记-动态规划

爱创课堂每日一题九十六天- 行内元素有哪些?块级元素有哪些? 空(void)元素有

爱创课堂每日一题九十九天- 行内元素有哪些?块级元素有哪些? 空(void)元素有

leetcode刷题三十六