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

  Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbers from the original list.

Example 1:

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

Example 2:

  Input: 1->1->1->2->3
  Output: 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         if not head or not head.next:  # 为空和只有一个节点直接返回
10             return head
11         res, cur = ListNode(0), head    # 设置哨兵节点
12         pre = res
13         while pre and cur:                # 循环结束条件
14             while cur.next and cur.val == cur.next.val:   # 判断该节点和下一个节点是否相等
15                 tem1= cur.val
16                 while cur and cur.val == tem1:           # 一直遍历到下一个和该节点不相等为止。
17                     cur = cur.next
18                 if not cur:             # 是否到为尾节点了
19                     pre.next = cur
20                     return res.next      # 直接返回
21             pre.next = cur             # 说明当前节点与下一个节点不相等,移动pre和cur指针为止
22             pre = pre.next
23             cur = cur.next
24         return res.next                 # 返回节点位置

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

时间: 2024-08-01 09:28:33

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

Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

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->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3.

LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重复数字,并且返回不重复数字的个数 遍历操作: 可以使用新的for循环 for (int n : nums){} 每次进行对比,并且更新第一个遇到不相等的元素的下标为i 对数组进行重新赋值操作 当数组长度大于1时,ans初值为1,当数组长度为0时,返回0 参考代码 : package leetcode_5

leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的重复II) 解题思路和方法

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->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2->3

leetcode_80题——Remove Duplicates from Sorted Array II(两个指针)

Remove Duplicates from Sorted Array II Total Accepted: 38480 Total Submissions: 125235My Submissions Question Solution Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,

LeetCode 80. Remove Duplicates from Sorted Array 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't

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

leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的反复II) 解题思路和方法

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->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2->3

leetcode:82. Remove Duplicates from Sorted List II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50411033 题目地址:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have