leetcode 【 Remove Duplicates from Sorted List 】 python 实现
题目:
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
.
代码:
# 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.next while p.next is not None:
if p.val == p.next.val:
p.next = p.next.next
else:
p = p.next return dummyhead.next
思路:
设立虚表头dummyhead
遇上val不同的,再执行 p=p.next;否则指针p不动,只是p.next变化。
leetcode 【 Remove Duplicates from Sorted List 】 python 实现的更多相关文章
- [leetcode]Remove Duplicates from Sorted List @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/ 题意: Given a sorted linked ...
- leetcode Remove Duplicates from Sorted Array python
class Solution(object): def removeDuplicates(self,nums): if len(nums) <= 0: return 0 j=0 for i in ...
- LeetCode:Remove Duplicates from Sorted List I II
LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [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 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [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 List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
随机推荐
- CRM, C4C和Hybris的后台作业
CRM 使用事务码SM36查看CRM系统的后台作业: 举一些例子: ABAP_TEXT_INDEX这个job执行的report是ABAP_DOCU_CREATE_TEXT_INDEX: 负责填充buf ...
- Leetcode 78. Subsets (backtracking) 90 subset
using prev class Solution { List<List<Integer>> res = new ArrayList<List<Integer&g ...
- 【CCPC-Wannafly Winter Camp Day4 (Div1) F】小小马(分类讨论)
点此看题面 大致题意: 给你一张\(n*m\)的棋盘,问你一匹马在两个点中是否存在一条经过黑白格子数目相等的路径. 简化题目 首先,我们来简化一下题目. 考虑到马每次走的时候,所经过的格子的颜色必然发 ...
- Codeforces 760A Petr and a calendar
题目链接:http://codeforces.com/problemset/problem/760/A 题意:日历需要多少列. #include <bits/stdc++.h> using ...
- Java小吐槽
简单说明,所有小吐槽都基于我的.NET经验,作为Java初学者,肯定有贻笑大方之处,欢迎之处,共同学习,共同进步. 1. The public type XXXXXXXX must be define ...
- 递归遍历目录拷贝cdh下的lib到一个目录
destpath='/home/hadoop/soft/hadoop-2.0.0-cdh4.5.0/cdhlib/'jarpath='/home/hadoop/soft/hadoop-2.0.0-cd ...
- 第41章 RS-485通讯实验—零死角玩转STM32-F429系列
第41章 RS-485通讯实验 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...
- HTML第三章:表单
第三章:表单 表单标签form:<form></form>//相当于一张记录用户信息的单子 常用属性:method:表单的提交方式,常用的值有两个 ...
- vue-wechat-title
html中的title安装:npm install vue-wechat-title --save1.在mian.js中//网页titleimport VueTitle from 'vue-wecha ...
- 15、SpringBoot------整合swagger2
开发工具:STS 前言: 对外提供一个Api,无论是对开发.测试.维护,都有很大的帮助. 下面我们来实现swagger2. 参考实例:https://blog.csdn.net/weixin_3947 ...