作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/remove-duplicates-from-sorted-list/

Total Accepted: 114584 Total Submissions: 311665 Difficulty: Easy

题目描述

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

题目大意

删除有序列表中的重复节点。

解题方法

判断相邻节点是否相等

如果下一个元素和这个元素的值相等。这个元素的下个元素就等于下个元素的下个元素。再循环就好了。

在找到不同的元素之前,当前元素不走。找到之后再走。

Java代码如下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode move=head;
while(move!=null && move.next!=null){
if(move.next.val == move.val){
move.next=move.next.next;
}else{
move=move.next;
}
} return head; }
}

这个做法的Python解法如下:

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head: return None
prev, cur = head, head.next
while cur:
if cur.val == prev.val:
prev.next = cur.next
else:
prev = cur
cur = cur.next
return head

C++版本如下:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (!head || !head->next) return head;
ListNode* prev = head;
ListNode* cur = head->next;
while (cur) {
if (prev->val == cur->val) {
prev->next = cur->next;
} else {
prev = cur;
}
cur = cur->next;
}
return head;
}
};

使用set

二刷, python

二刷第一遍没看清,不知道是个已经排序了的linkedlist,所以,用了set。没想到这个方法竟然更快点。

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head or not head.next: return head
val_set = set()
val_set.add(head.val)
root = ListNode(0)
root.next = head
while head and head.next:
if head.next.val in val_set:
head.next = head.next.next
else:
head = head.next
val_set.add(head.val)
return root.next

使用列表

使用一个类似于栈的列表,把每个链表节点向里面放,如果遇到了同样的元素那么“退栈”,只放入最后一个节点。可以这样做的原因是给出的链表是已经排序的,因此相等的节点会连续存在。

python代码如下:

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head or not head.next: return head
deque = []
move = head
while move:
while deque and deque[-1].val == move.val:
deque.pop()
deque.append(move)
move = move.next
for i in range(len(deque) - 1):
deque[i].next = deque[i + 1]
return deque[0]

递归

递归解法还不是那么好想的,需要返回的是当前节点或者下一个节点,也就是说如果重复的时候,舍弃掉当前节点。

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
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 if head.val != head.next.val else head.next

日期

2016/5/1 15:05:24
2018 年 6 月 23 日 —— 美好的周末要从刷题开始
2018 年 11 月 20 日 —— 真是一个好天气
2019 年 9 月 16 日 —— 秋高气爽

【LeetCode】83. Remove Duplicates from Sorted List 解题报告(C++&Java&Python)的更多相关文章

  1. [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项

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

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

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

  3. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  4. LeetCode 83. Remove Duplicates from Sorted List (从有序链表中去除重复项)

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

  5. Java [Leetcode 83]Remove Duplicates from Sorted List

    题目描述: Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

  6. [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)

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

  7. [leetcode]83. Remove Duplicates from Sorted List有序链表去重

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

  8. [LeetCode] 83. Remove Duplicates from Sorted List_Easy tag: Linked List

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

  9. [LeetCode]83. Remove Duplicates from Sorted List(排序链表去重)

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

随机推荐

  1. 认识Influxdb时序数据库及Influxdb基础命令操作

    认识Influxdb时序数据库及Influxdb基础命令操作 一.什么是Influxdb,什么又是时序数据库 Influxdb是一个用于存储时间序列,事件和指标的开源数据库,由Go语言编写而成,无需外 ...

  2. 毕业设计之mysql+主从复制+keepalived

    环境介绍 mysql_VIP:192.168.111.123 mysql_M!:192.168.111.151 mysql_M2:192.168.111.152 安装mysql可以查看 两个数据库都需 ...

  3. Linux—export命令查看、修改用户环境变量

    Linux export 命令用于设置或显示环境变量. 在 shell 中执行程序时,shell 会提供一组环境变量. export 可新增,修改或删除环境变量,供后续执行的程序使用. export ...

  4. TCP三次握手与Linux的TCP内核参数优化

    感谢各位技术大佬的资料分享,这里我把我理解的内容做一个整理 一:TCP的三次握手 1.TCP简述 TCP是一个面向连接的协议,在连接双方发送数据之前,首先需要建立一条连接.TCP建立连接可以简单称为: ...

  5. Python获取随机数

    Python当中,可用random模块来获取随机数 import random """ random模块,用于获取随机数 """ print ...

  6. SpringBoot整合Shiro 一:搭建环境

    Java项目的安全框架一般使用 shiro 与 spring security 具体怎么选择可以参考文章:安全框架 Shiro 和 Spring Security 如何选择 我这里选择使用Shiro ...

  7. C++中union相关

    前两天做阿里笔试遇到一个选择题题目大概是 #include <iostream> #include <stdlib.h> using namespace std; union ...

  8. zabbix之邮件报警

    创建媒介类型 如果用QQ邮箱的话,先设置一下授权码 为用户设置报警 创建一个用户 配置动作 测试

  9. redis入门到精通系列(八):redis的高可用--主从复制详解

    (一)主从复制介绍 前面所讲的关于redis的操作都属于单机操作,单机操作虽然操作简单,但是处理能力有限,无法高可用.所谓高可用性,就是指当一台服务器宕机的时候,有备用的服务器能顶替上,在单机操作上这 ...

  10. Synchronized深度解析

    概览: 简介:作用.地位.不控制并发的影响 用法:对象锁和类锁 多线程访问同步方法的7种情况 性质:可重入.不可中断 原理:加解锁原理.可重入原理.可见性原理 缺陷:效率低.不够灵活.无法预判是否成功 ...