【LeetCode每天一题】Remove Duplicates from Sorted List(移除有序链表中的重复数字)

  Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

思路

  对于链表类的题目主要考的是指针的操作,他需要对指针的指向节点有正确的操作。这道题我们可以使用从头开始遍历,每当一个新节点时,都对该节点后面的节点进行比较是否相等,一直移动到和该节点不相等的节点为止,然后改变指针的指向跳过重复的元素。以直到末尾为止。这里需要主要循环结束条件等问题。时间复杂度为O(n),空间复杂度为O(1)。解决代码



 1 # Definition for singly-linked list.
 2 # class ListNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution(object):
 8     def deleteDuplicates(self, head):
 9         """
10         :type head: ListNode
11         :rtype: ListNode
12         """
13         if not head:    # 空节点直接返回
14             return head
15         pre = head
16         while pre:
17             if pre.next and pre.val == pre.next.val: # 判断当前节点的后序节点是否相等。
18                 tem, cur = pre.val, pre.next
19                 while cur and cur.val == tem:     # 循环移动一直到和当前节点不相等的为止。
20                     cur = cur.next
21                 pre.next = cur            # 指向后面一个和当前节点不相等的节点
22                 if not cur:                 # 为空说明遍历完毕直接返回
23                     return head
24                 continue
25             pre = pre.next
26
27         return head                    # 返回结果



原文地址:https://www.cnblogs.com/GoodRnne/p/10799528.html

时间: 2024-08-29 14:04:32

【LeetCode每天一题】Remove Duplicates from Sorted List(移除有序链表中的重复数字)的相关文章

Remove Duplicates from Sorted 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. 移除有序链表中的重复项需要定义个指针指向该链表的第一个元素,然后第一个元素和第二个元素比较,如果重复了,则删掉第二个元素,

[CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项

2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed? 这道题让我们移除无序链表中的重复项,在LeetCode中有两道类似的题是Remove Duplicates from Sorted List 移除有序链表中的重复项 和 Remove Duplicates fr

[LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby 描述 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1

[LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)

①中文题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1: 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2. 你不需要考虑数组中超出新长度后面的元素.示例 2: 给定 nums = [0,0,1,1,1,2,2,3,3,4], 函数应该返回新的长度 5, 并且原数组

力扣—Remove Duplicates from Sorted List(删除排序链表中的重复元素)python实现

题目描述: 中文: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2 示例 2: 输入: 1->1->2->3->3输出: 1->2->3 英文: Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->

【LeetCode-面试算法经典-Java实现】【082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)】

[082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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->

leetCode 80.Remove Duplicates from Sorted Array II (删除排序数组中的重复II) 解题思路和方法

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn'

leetcode第26题--Remove Duplicates from Sorted Array

problem: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input ar

leetcode第83题-Remove Duplicates from Sorted List

这道题与实现数组中的删除重复元素类似.我们来看一下具体的过程,首先要判断是否为空(在这个上面吃了大苦头),删除下一个相同的元素的时候要定义一个tmp元素,再free掉即可. #include<stdio.h> #include<stdlib.h> struct ListNode { int val; ListNode *next; }; ListNode *deleteDuplicates(ListNode *head) { if (head) { struct ListNode