leetcode 【 Remove Duplicates from Sorted List II 】 python 实现
题目:
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
代码:oj在线测试通过 288 ms
- # Definition for singly-linked list.
- # class ListNode:
- # def __init__(self, x):
- # self.val = x
- # self.next = None
- class Solution:
- # @param head, a ListNode
- # @return a ListNode
- def deleteDuplicates(self, head):
- if head is None or head.next is None:
- return head
- dummyhead = ListNode(0)
- dummyhead.next = head
- p = dummyhead
- while p.next is not None and p.next.next is not None:
- tmp = p
- while tmp.next.val == tmp.next.next.val:
- tmp = tmp.next
- if tmp.next.next is None:
- break
- if tmp == p:
- p = p.next
- else:
- if tmp.next.next is not None:
- p.next = tmp.next.next
- else:
- p.next = tmp.next.next
- break
- return dummyhead.next
思路:
设立虚表头 hummyhead 这样处理Linked List方便一些
p.next始终指向待比较的元素
while循环中再嵌套一个while循环,把重复元素都跳过去。
如果遇到了重复元素:p不动,p.next变化;如果没有遇到重复元素,则p=p.next
Tips: 使用指针之前 最好加一个逻辑判断 指针不为空
leetcode 【 Remove Duplicates from Sorted List II 】 python 实现的更多相关文章
- [leetcode]Remove Duplicates from Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- [leetcode]Remove Duplicates from Sorted List II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...
- Leetcode: Remove Duplicates from Sorted List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [LeetCode] Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode::Remove Duplicates from Sorted List II [具体分析]
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- 【2017-06-20】Linux应用开发工程师C/C++面试问题记录之一:Linux多线程程序的同步问题
参考之一:Linux 线程同步的三种方法 链接地址:http://www.cnblogs.com/eleclsc/p/5838790.html 简要回答: Linux下线程同步最常用的三种方法就是互斥 ...
- Jerry Wang诚邀广大SAP同仁免费加入我的知识星球,共同探讨SAP技术问题
大家知道Jerry Wang有一个微信公众号"汪子熙",2017年12月27日,Jerry的这个公众号发布了第一篇文章.到今天2018年10月底为止,正好十个月. 在这10个月的时 ...
- CRM和C4C product category hierarchy的可编辑性控制逻辑
CRM 从ERP导入到CRM系统的Product Hierarchy,在CRM系统切换成编辑模式时,会收到一条提示信息: Hierarchy XXX may only be changed in th ...
- while循环小例
# 使用while 循环输入 1 2 3 4 5 6 8 9 10 n = 1 while n <= 10: if n == 7: pass else: print(n) n = n + 1 # ...
- 模仿ArcGIS用Graphics重绘的直方图分级调节器
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 【转】VMware虚拟机系统无法上网怎么办?
有很多用户通过安装VMware软件来创建虚拟机系统,其中就有部分用户在创建好虚拟机系统后遇到无法上网的问题,下面PC6苹果网小编就给大家带来VMware虚拟机系统下无法上网的解决办法: 1.在虚拟机右 ...
- Yarn下Map数控制
public List<InputSplit> getSplits(JobContext job) throws IOException { long minSize = Math.max ...
- Win7多用户同时登陆
软件提供下载: http://pan.baidu.com/s/1o6FQv70
- oracle: listener.ora 、sqlnet.ora 、tnsnames.ora的配置及例子
1.解决问题:TNS或者数据库不能登录. 最简单有效方法:使用oracle系统提供的工具 netca 配置(把原来的删除掉重新配置) $netca 2.然而,仍有疑问:如何指定'l ...
- Linux笔记(开机自动将kerne log保存到SD卡中)
有时候为了测试机器的稳定性,需要煲机测试几天的情况,这个时候机器已经封装好,不能再接串口线出来. 为了追溯问题,就需要将log信息保存下来. 于是就需要这样一个功能:系统启动后,自动将kernel的l ...