[LeetCode]题解(python):082 - Remove Duplicates from Sorted List II
题目来源
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
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.
题意分析
Input:
:type head: ListNode
Output:
:rtype: ListNode
Conditions:与83题不同,只要元素出现过,则将该元素去掉
题目思路
因为list是有序的,并且可能返回一个空list,所以增加一个头节点。再增加一个节点时,就看这个节点之后是否有值与这个节点的值重复,如果有就不加这个值的【所有】节点
AC代码(Python)
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None or head.next == None:
return head ans = ListNode(-1)
ans.next = head
p = ans
temp = p.next while p.next:
while temp.next and temp.next.val == p.next.val:
temp = temp.next
if p.next == temp:
p = p.next
temp = p.next
else:
p.next = temp.next return ans.next
[LeetCode]题解(python):082 - Remove Duplicates from Sorted List II的更多相关文章
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- Java for LeetCode 082 Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 082 Remove Duplicates from Sorted List II 有序的链表删除重复的结点 II
给定一个有序的链表,删除所有有重复数字的节点,只保留原始列表中唯一的数字.例如:给定 1->2->3->3->4->4->5 ,则返回 1->2->5给 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)
82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【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 && Remove Duplicates from Sorted List II
1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...
随机推荐
- BZOJ3103 : Palindromic Equivalence
用Manacher可以推出O(n)对相等和不等关系. 将相等的用并查集维护,不等的连边. 然后从1到n,如果该等价类还没被考虑过,则ans*=26-与它不等的考虑过的等价类个数. #include&l ...
- BZOJ3750 : [POI2015]Pieczęć
枚举第一个位置,然后暴力检验. #include<cstdio> #define N 1010 int T,n,m,a,b,x,y,i,j,k,q[N*N][2],cnt;char s[N ...
- 【wikioi】1108 方块游戏(模拟)
http://wikioi.com/problem/1108/ 这题有点变态,因为他根本没有策略! 还是说这题不是实时的?反正这题很变态,是在一个时间段同时消除所有的行列斜边,同一时间!!!!!! 所 ...
- ejabberd 的框架
最近看源码,总结ejabberd的大致框架如下
- 给NSString增加Java风格的方法
给NSString增加Java风格的方法 文章目录 我实在受不了 NSString 冗长的方法调用了,每次写之前都要查文档.特别是那个去掉前后多余的空格的方法,长得离谱.与之对应的别的语言,拿 jav ...
- MUI - 手势
www.bcty365.com/content-146-2389-1.html 配置事件 mui.init({ gestureConfig: { hold: true, //长按屏幕,默认是false ...
- [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法
本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...
- 通过sqlplus 登录数据库服务器
点击“运行”,输入"sqlplus",弹出 , 再往里面输入账号scott,密码tiger 或者,点击“运行”,输入"sqlplus /nolog",此时只打开 ...
- debug阶段工作期站立会议2(进度推进)
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- HTML5 :b/strong加粗,i/em倾斜区别
解释1 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="ut ...