Remove Duplicates from Sorted List - LeetCode
来源:互联网 发布:淘宝网优惠券领取 编辑:程序博客网 时间:2024/05/19 08:43
Remove Duplicates from Sorted List - LeetCode
题目:
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
.
分析:
这道题目应该就是考一个基本的链表删除,没什么难度,我用一个变量now记录现结点的val,若与下一个结点的val不一样,则把now替换成现结点的val;否则用
delete函数删除现结点,并且跳到下一个结点。
代码:
class Solution: # @param head, a ListNode # @return a ListNode def deleteDuplicates(self, head): now = 0 prev = head temp = head while head is not None: if now != head.val: now = head.val prev = head head = head.next else: head = self.delete(prev, head) return temp def delete(self,prev,head): prev.next = head.next del head return prev.next
0 0
- Remove Duplicates from Sorted List :from LeetCode
- Leetcode - List - Remove Duplicates from Sorted List
- [leetcode][list] Remove Duplicates from Sorted List
- [leetcode][list] Remove Duplicates from Sorted List
- LeetCode: Remove Duplicates from Sorted List
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- leetcode 37: Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List
- 多种UI控件实现代码
- SecureCRT&SecureFX_v7.2.5_x86&x64_build_524 破解安装教程
- Spring学习 7
- C4996 ---- _CRT_SECURE_NO_WARNINGS
- 设计模式六大原则 单一职责原则
- Remove Duplicates from Sorted List - LeetCode
- Pig Iron Stocks in India
- java语言程序设计-基础篇-4.21习题
- 西裤去污技巧及注意事项—上海西服定制哪家好?顾杨西服
- C++练习题——寻找子字符串
- 《设计模式之禅》——桥梁模式
- Hibernate之缓存机制
- linux下find(文件查找)命令的用法总结
- 关于BeginSheet 的窗口问题