`

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 
        

 

分享到:
评论

相关推荐

    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题目+解析+思路+答案.pdf

    - **Remove Duplicates from Sorted List**:从已排序的链表中移除重复项。 - **Merge Sorted Lists**:合并两个已排序的链表。 - **Reverse Linked List**:反转链表。 - **Swap Nodes in Pairs**:交换链表中...

    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-...

    LeetCode leetcode部分题解答代码实现

    * Remove Duplicates from Sorted List:给定一个已排序的链表,移除重复元素,并返回新链表。这个题目需要使用快慢指针的思想,将链表分解成更小的子链表,并移除重复元素。 8. 数学 数学是一种非常重要的知识...

    LeetCode刷题题解答案(c++).pdf 彻底搞懂了编程算法题,成功拿到了大厂offer!

    1. **去除排序数组中的重复项(Remove Duplicates from Sorted Array)** - **问题描述**:给定一个排序数组,在原地删除重复出现的元素,使得每个元素只出现一次,并返回新的长度。不使用额外数组空间。 - **解决...

    LeetCode练习答案

    - **移除排序数组中的重复项(Remove Duplicates from Sorted Array)**: 给定一个已排序的数组,去除重复元素并保持原数组顺序,返回处理后数组的新长度。 - **加一(Plus One)**: 给定一个非负整数表示为非空的整数数...

    小象学院2020 刷题班 3.2-4.171

    11. **删除链表重复元素**(Remove_duplicates_from_sorted_list_II) - 知识点:链表,双指针 - 解题方法:使用两个指针,一个指向当前节点,另一个指向前一个节点,当发现连续重复时,删除当前节点。 12. **...

    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 ...

Global site tag (gtag.js) - Google Analytics