Remove Duplicates from Sorted List II
2015-06-18 11:24
471 查看
题目:
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given
Given
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param {ListNode} head
# @return {ListNode}
def deleteDuplicates(self, head):
sentry = ListNode(-1)
sentry.next = head
pre,now = sentry,head
while now and now.next:
if now.val==now.next.val:
now = now.next
while now.next and now.val==now.next.val:
now=now.next
now = now.next
pre.next = now
else:
pre = now
now = now.next
return sentry.next
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.
代码:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param {ListNode} head
# @return {ListNode}
def deleteDuplicates(self, head):
sentry = ListNode(-1)
sentry.next = head
pre,now = sentry,head
while now and now.next:
if now.val==now.next.val:
now = now.next
while now.next and now.val==now.next.val:
now=now.next
now = now.next
pre.next = now
else:
pre = now
now = now.next
return sentry.next
相关文章推荐
- android studio 目录结构
- 笔记——【C和指针】
- 大型网站架构改进历程:存储的瓶颈
- 数据结构与算法分析 L5
- ofbiz中的ofbiz-component.xml和加载过程
- 云计算容器服务该何去何从
- 投影仪矫正
- 《构建之法》--阅读(第13章-第17章)
- 绑定自己Self
- 13、14、15、16、17
- Python中的base64、base32实例
- 将数据库某列改为自增长
- 认识.NET
- Java_增强for循环
- ]9.zookeeper原理解析-选举之QuorumPeerMain加载
- NDK编译X265库到ARM,遇到问题
- 替换字符串String中的元素和分割字符串为数组
- 《构建之法》13,14,15,16,17章读后感
- 【犀利评】看完你能乐得多吃一碗饭
- ORACLE常用命令