83. Remove Duplicates from Sorted List
来源:互联网 发布:河北中科恒运软件 编辑:程序博客网 时间:2024/06/10 15:55
题意: 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.
代码:
def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ # h = ListNode(float('-inf')) # h.next = head if not head: return head p = head while p.next: # print p.val if p.val == p.next.val: p.next = p.next.next else: p = p.next return head
参考代码:
def deleteDuplicates(self, head): cur = head while cur: while cur.next and cur.next.val == cur.val: cur.next = cur.next.next # skip duplicated node cur = cur.next # not duplicate of current node, move to next node return head
还有一个递归的code:
def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ if not head or not head.next: return head head.next = self.deleteDuplicates(head.next) return head.next if head.val == head.next.val else head
0 0
- [LeetCode]83.Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- [Leetcode] 83. Remove Duplicates from Sorted List
- LeetCode --- 83. Remove Duplicates from Sorted List
- [leetcode] 83.Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- LeetCode 83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- 【leetcode】83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List LeetCode
- 83. Remove Duplicates from Sorted List
- leetcode 83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- [LeetCode]83. Remove Duplicates from Sorted List
- 【LeetCode】83. Remove Duplicates from Sorted List
- LeetCode 83. Remove Duplicates from Sorted List
- iTextSharp 纸张横向
- 分享个阿里通信图标库
- 如何用网络管理软件灵活管理复杂庞大的网络
- BitSet 设置及取值 求交集和求并集
- 将DataGridView选中行对象(DataGridViewRow)转为DataTable的行对象
- 83. Remove Duplicates from Sorted List
- 生活的智慧
- linux:查询路径下的包含某字符串的所有文件的命令
- <portlet:namespace/>的作用和用法
- 实验2——建模
- mysql安装文档
- nth-child(n)和nth-of-type(n)的区分
- 安装gcc-6系列与安装boost (Ubuntu16.04LTS)
- Java正则表达式Pattern和Matcher