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 ...
随机推荐
- web的攻击技术
简单的http协议本身并不存在安全性问题,因此技术本身几乎不会成为攻击的对象,应用http协议的服务器和客户端,以及运行在服务器端web应用等资源才是攻击目标,那么怎么攻击,来源于哪里呢 web应用攻 ...
- 2018.7.18 div,section,article的区别和使用
section ·<section> 标签定义文档中的节(section.区段).比如章节.页眉.页脚或文档中的其他部分. ·section用作一段有专题性的内容,一般在它里面会带有标题. ...
- 2017.11.5 Java Web ----案例:数据库访问JavaBean的设计
(12)案例----数据库访问JavaBean的设计 例题:数据库操作在一个Web应用程序中的后台处理中占有大比重,设计一组JavaBean封装数据库的基本操作供上层模块调用,提高程序的可移植性. [ ...
- Unable to launch the Java Virtual Machine
看看国内的回答,http://zhidao.baidu.com/question/119993351.html 再看看国外的,http://www.mkyong.com/oracle/oracle-s ...
- spring中使用i18n(国际化)
简单了解i18n i18n(其来源是英文单词internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版物,软件,硬件 ...
- wgan pytorch,pyvision, py-faster-rcnn等的安装使用
因为最近在读gan的相关工作,wgan的工作不得不赞.于是直接去跑了一下wgan的代码. 原作者的wgan是在lsun上测试的,而且是基于pytorch和pyvision的,于是要装,但是由于我们一直 ...
- jquery 添加和删除节点
// 增加一个三和一节点 function addPanel() { // var newPanel = $('.my-panel').clone(true) var newPanel = $(&qu ...
- Javascript和HTML5的关系
HTML5是一种新的技术,就目前而言,我们所知的HTML5都是一些标签,但是有了JS之后,这些标签深层的扩展功能才得以实现. 比如video标签,我们对其理解为一个简单的标签,但实际上,v ...
- Percona-Tookit工具包之pt-pmp
Preface Sometimes we need to know the details of a program(porcess) when it is running even ...
- MySQL的where条件优化
where 条件优化 适合select delete update 1.避免无用的括号 ((a AND b) AND c OR (((a AND b) AND (c AND d)))) -> ...