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 ...
随机推荐
- java文件
File类 为了很方便的代表文件的概念,以及存储一些对于文件的基本操作,在java.io包中设计了一个专门的类——File类. 在File类中包含了大部分和文件操作的功能方法,该类的对象可以代表一个具 ...
- Linux常用命令之文件和目录操作命令
以下是linux操作系统常用的文件和目录操作命令: cd /home 进入 '/ home' 目录' cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd 进入个人的主目录 cd ...
- POJ-2456 Aggressive cows---最大化最小值(也就是求最大值)
题目链接: https://vjudge.net/problem/POJ-2456 题目大意: 有n个牛栏,选m个放进牛,相当于一条线段上有 n 个点,选取 m 个点, 使得相邻点之间的最小距离值最大 ...
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 【转载】刘昕明:送给和我一样曾经浮躁过的PHP程序员
刘昕明:送给和我一样曾经浮躁过的PHP程序员 来源:刘昕明博客 作者:刘昕明 2012年偶决定开始写博客了,不为别的,就希望可以通过博客记录我的成长历程同时也希望可以帮助一些刚毕业,刚 ...
- Mac安装protobuf 流程
下载 https://github.com/google/protobuf/releases 找到对应版本下载 编译 cd protobuf./autogen.sh./configuremake 安装 ...
- C#接口定义
C#接口定义 C#不支持多重继承,但是客观世界出现多重继承的情况又比较多.为了避免传统的多重继承给程序带来的复杂性等问题,C# 提出了接口的概念.通过接口可以实现多重继承的功能. 继承该接口的类或结 ...
- Login用户登录(Python实现)
username_fault = "isuperSun" #程序存储用户名和密码 password_fault = " counts = 0 while counts&l ...
- SAP BI 常用TCODE
S.No Tcode Description 1 RSA1 Administrator Work Bench 2 RSA11 Calling up AWB with the IC tree 3 RSA ...
- (二)、Python 基础
Python入门 一.第一句Python 在 /home/dev/ 目录下创建 hello.py 文件,内容如下: print "hello,world" 执行 hello.py ...