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 ...
随机推荐
- 出现Permission denied的解决办法
出处 https://blog.csdn.net/qq_16525279/article/details/80245350 提示 Permission denied 解决的办法: $ sudo chm ...
- 小程序wx.request的POST方法的参数传输服务器接收不到
这是API里面的例子: 而实际这样,服务端拿到的是空值. 将header更改一下,application/x-www-form-urlencoded,则可以让服务器收到数据
- ArcGIS License Server Administrator 10.2 无法启动许可的解决办法
刚刚重装了电脑,安装ArcGIS的时候,安装完desktop之后又安装了License Manager,结果把破解文件替换完之后,发现ArcGIS License Server Administrat ...
- 解析xml文件,修改Jenkins的配置
最近因为服务器移动,在Jenkins中配置的一些地址之类的,都要改变,如图,我因为使用插件Sidebar Links增加一个链接地址,现在地址变了,所以在Jenkins中配置就需要改动link url ...
- Android OpenGL ES 画球体
近期由于兴趣所向.開始学习OpenGL绘图. 本文以"画球体"为点,小结一下近期所学. > 初识OpenGL ES 接触OpenGL是从Android開始的.众所周知,And ...
- 二叉索引树,LA2191,LA5902,LA4329
利用了二进制,二分的思想的一个很巧妙的数据结构,一个lowbit(x):二进制表示下的最右边的一个1开始对应的数值. 那么如果一个节点的为x左孩子,父亲节点就是 x + lowbit(x),如果是右孩 ...
- c# base new 等关键字基础
base关键字 不仅可以 调用父类的 实例方法,也能狗调用父类的 构造方法 https://www.cnblogs.com/aehyok/p/3519599.html
- 利用python的numpy创建矩阵并对其赋值
创建一个3X3的矩阵并对其赋值: x = numpy.array([[1,2,3],[4,5,6],[7,8,9]]) print x print x.shape 运行结果: [[ ] [ ] [ ] ...
- Python 二进制 八进制 十进制 十六进制
1.四种进制的表示方式 >>> 0b010 0b二进制 >>> 0x010 0x 十六进制 >>> 0o010 0o 八进制 >>&g ...
- Mybatis-generator自动生成
第一步:导入架包 <build> <plugins> <plugin> <groupId>org.mybatis.generator</group ...