`

Remove Duplicates from Sorted List——Linked List

 
阅读更多

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(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        t = node.next
        node.val = node.next.val 
        node.next = node.next.next

    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        r = head 
        while head is not None and head.next is not None:
        	if head.val == head.next.val:
        		self.deleteNode(head)
        	else:
        		head = head.next 
        return r 
        

 

分享到:
评论

相关推荐

    cpp-算法精粹

    Remove Duplicates from Sorted List II Rotate List Remove Nth Node From End of List Swap Nodes in Pairs Reverse Nodes in k-Group Copy List with Random Pointer Linked List Cycle Linked List Cycle II ...

    leetcode2sumc-LeetCode:LeetCode的一些题目

    Duplicates from Sorted List 141 Easy Linked List Cycle 160 Easy Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List

    javalruleetcode-JavaInterviewChecklist:要检查的Java事项

    Duplicates from Sorted List Palindrome Linked List LL中的插入排序 使用额外的缓冲区从未排序的链表中删除重复项 细绳 确定字符串是否包含所有唯一字符 (CTCI) 在不使用额外缓冲区的情况下删除字符串中的重复字符...

    leetcode2sumc-ack-CherishLeetCode:ack-CherishLeetCode

    leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack ...Duplicates from Sorted List 141 * Linked List Cycle 160 * Intersection of Two Linke

    leetcode2sumc--Offer:-提供

    leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack ...Duplicates from Sorted List 141 * Linked List Cycle 160 * Intersection of Two Linke

    leetcode-liwang:leetcode学习笔记

    O(m+n) time, O(m+n) sapce.*0026 Remove Duplicates from Sorted Array使用双指针,一个快指针,一个慢指针。开始时,两个指针都指向首元素。当两指针元素值相同时,快指针+1;当两指针元素不同时,慢

    javalruleetcode-leetcode-java:力码笔记

    26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141....

    lrucacheleetcode-LeetCode:LeetCode刷题

    II(Remove Duplicates from Sorted List II) 2018.9.27 重建二叉树(Rebuild Binary Tree) 2018.9.28 把字符串转换成整数(Convert a string to an integer) 2018.10.8 树的子结构(Substructure of the tree) ...

    leetcodepython001-LeetCode:力码

    leetcode python ...Linked-list 002 Add Two Numbers Stack 020 Valid Parenthesis Hash Table 001 TwoSum Reference 完整的学习流程 How to be a softwair engineer: 其他人详解 Python的各式演算法

    :猴子:LeetCode,剑指提供刷题笔记(C / c++, Python3实现)

    LeetCode 原创文章每周最少两篇,后续最新文章会在首发,视频首发,大家可以加我进交流群,技术交流或提意见都可以,欢迎Star! 帮助文档 帮助文档存放在Help文件夹下。...Remove Duplicates from Sorted Lis

    leetcode答案-leetcode-java:leetcode的Java代码

    leetcode 答案leetcode-java ...的 Java 答案 ================索引=...Duplicates from Sorted List com.leetcode.string Single Number com.leetcode.tree Balanced Binary Tree Maximum Depth of Binary Tree Same Tree

    leetcode中325题python-leetcode:leetcode

    remove-duplicates-from-sorted-list ii 83 删除排序链表中的重复元素 remove-duplicates-from-sorted-list 86 分隔链表 partition-list 92 反转链表 II reverse-linked-list-ii(Reverse a Sub-list) 141 环形链表...

    LeetCode最全代码

    26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates...

    leetcode分发糖果-ForDaFactory:使用C++的个人leetcode解决方案

    83-删除排序链表中的重复元素:remove-duplicates-from-sorted-list 92-反转链表II:reverse-linked-listt-ii 141-环形链表:linked-list-cycle 142-环形链表:linked-list-cycle-ii 160-相交链表:intersection-of-two-...

    BobBuilder_app

    As you can see the page list is a sorted dictionary of first keys from each page along with associated page number and page items count. A page is a dictionary of key and record number pairs. This ...

    微软内部资料-SQL性能优化5

    The data pages in the table are kept in a doubly linked list called the page chain. The order of pages in the page chain, and the order of rows on the data pages, is the order of the index key or ...

Global site tag (gtag.js) - Google Analytics