• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

nunca

但行好事 莫问前程
  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

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

  

既然无论如何时间都会过去,为什么不选择做些有意义的事情呢

posted on 2018-04-01 15:48  乐晓东随笔  阅读(120)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3