题目描述

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

思路:

如果该链表当前节点与下一节点为空,则返回前当前节点。

否则,比较这两个节点的val,使用递归,

如果 当两节点值相等时,使用temp来替代 pHead.next

然后循环判断temp是否为空,若不为空,则temp指向下一节点。

如果不相等,则移动到下一节点

# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplication(self, pHead):
# write code here
if not pHead or not pHead.next:
return pHead
if pHead.val == pHead.next.val:
temp = pHead.next
while temp and temp.val == pHead.val:
temp = temp.next
return self.deleteDuplication(temp)
else:
pHead.next = self.deleteDuplication(pHead.next)
return pHead
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
thead = ListNode('a')
thead.next = head
pre,cur = None,thead
while cur:
pre=cur
cur=cur.next
while cur and cur.next and cur.next.val == cur.val:
t=cur.val
while cur and cur.val==t:
cur=cur.next
pre.next=cur
return thead.next

最新文章

  1. 仿淘宝分页按钮效果简单美观易使用的JS分页控件
  2. my sql中join的操作
  3. De4Dot+Reflector 支持多种反混淆
  4. canvas 画六边形
  5. iis7.5错误 配置错误
  6. Cocos2d入门--2--三角函数的应用
  7. 使用sed,awk将love转换成LOVE,将CHINA转换成china
  8. Python:字典
  9. spring mvc mybatis集成踩的坑
  10. HTTPS与MITM
  11. 不高兴的o( ̄ヘ ̄o#)JJ
  12. javascript实现游戏贪吃蛇
  13. Disconnected from the target VM, address: '127.0.0.1:57178', transport: 'socket'
  14. [js] - 前端FileReader使用,适用于文件上传预览.(并未传入后端)
  15. WPF中的DoubleAnimation
  16. ES系列十二、ES的scroll Api及分页实例
  17. setBit testBit权限管理
  18. ElasticSearch 2 (20) - 语言处理系列之如何开始
  19. 1050: 贝贝的ISBN号码(isbn)
  20. JVM笔记(二) 垃圾收集器(1)

热门文章

  1. JUnit——单元测试
  2. tensorflow的变量作用域
  3. json 的简单应用
  4. [BZOJ2987]Earthquake:类欧几里得算法
  5. Min_25筛初级应用:求$[1,n]$内质数个数
  6. 学习日记21、IE下的Ajax需要注意的地方
  7. windows powershell的常用命令
  8. 6.并发编程--volatile
  9. 使用私有仓库(Docker Registry 2.0)管理镜像
  10. 读读《编写高质量代码:改善Java程序的151条建议》