• LeetCode Easy:83. Remove Duplicates from Sorted List


    一、题目

    Given a sorted linked list, delete all duplicates such that each element appear only once.

    For example,
    Given 1->1->2, return 1->2.
    Given 1->1->2->3->3, return 1->2->3.

    要求是删去有序链表中重复的元素。

    二、此题比较简单,设置一个游标遍历链表,只要比较游标的当前和next.value是否相同,相同的话就跳过,让当前游标指向next.next

    三、代码

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def deleteDuplicates(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            p = head
            while p!= None:
                while p.next != None and p.val == p.next.val:
                    p.next = p.next.next
                p = p.next
            return head
    

      

    既然无论如何时间都会过去,为什么不选择做些有意义的事情呢
  • 相关阅读:
    POJ 1887 Testing the CATCHER
    HDU 3374 String Problem
    HDU 2609 How many
    POJ 1509 Glass Beads
    POJ 1458 Common Subsequence
    POJ 1159 Palindrome
    POJ 1056 IMMEDIATE DECODABILITY
    POJ 3080 Blue Jeans
    POJ 1200 Crazy Search
    软件体系结构的艺术阅读笔记1
  • 原文地址:https://www.cnblogs.com/xiaodongsuibi/p/8686909.html
Copyright © 2020-2023  润新知