Remove Duplicates from Sorted List | & ||

时间:2022-12-13 21:11:39

Remove Duplicates from Sorted List I

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

Example

Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

分析:

Use one pointer called "current" to point to the head, and pointer "pointer" points to the next one. If the value of node pointed by "pointer" is the same with the value of the node pointed by pointer "current", we move pointer "pointer" to the next node, otherwise, current.next = pointer, and both pointers move to the next node.

 /**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
if (head == null || head.next == null) return head; ListNode current = head;
ListNode pointer = head.next; while (pointer != null) {
if (pointer.val != current.val) {
current.next = pointer;
current = pointer;
}
pointer = pointer.next;
}
current.next = null;
return head;
}
}

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.

Example

Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

分析:

This question is a little bit hard. It is because we need to move the pointer all the way down to the node whose value is not the same with the previous one.

a if and while loop combination can be used in this case.

 /**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of the linked list
*/ public static ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) return head; ListNode dummy = new ListNode();
ListNode current = dummy; while (head != null) {
if (head.next != null && head.val == head.next.val) {
int val = head.val;
while(head != null && head.val == val) {
head = head.next;
}
} else {
current.next = head;
current = current.next;
head = head.next;
current.next = null;
}
}
return dummy.next;
}
}

转载请注明出处:cnblogs.com/beiyeqingteng/

Remove Duplicates from Sorted List | & ||的更多相关文章

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

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

  2. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  5. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  6. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  7. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  10. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

随机推荐

  1. [BOT]自己动手实现android 饼状图,PieGraphView,附源码解析

    本文要介绍的是一个参照手机支付宝app里面记账本功能里的"饼状图"实现的控件.通常app中可能的数据展示控件有柱状图,折线图,饼状图等,如果需要一个包含多种View控件的库,那么 ...

  2. 【Python数据分析】从Web收集数据小实例

    最近在看<鲜活的数据:数据可视化指南>,学习一些数据可视化与数据分析的技术,本例是该书第一章的一个例子衍伸而来. 实例内容:从www.wunderground.com收集美国纽约州布法罗市 ...

  3. OOP

    class Worker(object): def __init__(self,name,bu,score,sex,age): self.__name = name self.__bu = bu se ...

  4. 使用属性表:VS2013上配置OpenCV

    以前,windows下配置OpenCV一直不太方便:总是要手动添加lib,添加include,还要配置PATH使得程序运行时候能找到dll文件. 每次新建一个使用OpenCV的工程都要手动添加,很麻烦 ...

  5. 查看MS Sqlserver文件大小语句

    1..查询数据库的数据文件及日志文件的相关信息(包括文件组.当前文件大小.文件最大值.文件增长设置.文件逻辑名.文件路径等) select * from [数据库名].[dbo].[sysfiles] ...

  6. json 不能 dumps Decimal 解决办法

    class DecimalEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, decimal.Decimal): ret ...

  7. MySQL之表连接(内外连接和重命名的使用)

    #要多练练 1.连接查询根据连接方式分为 内连接 等值连接 非等值连接 自连接 外连接 左外连接(左连接) 右外连接(右连接) 当多张表进行连接查询,若没有任何条件进行限制,会 发生什么现象? 会出现 ...

  8. Linux知识总汇

    Linux相关教程 Linux的安装以及基础配置 Linux上安装Python3 Linux上安装pip以及setuptools Linux上安装MySQL Linux上安装Django Linux上 ...

  9. Mybatis 动态Sql语句《常用》

    MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省掉 ...

  10. Cookies、sessionStorage和localStorage解释及区别?

    浏览器的缓存机制提供了可以将用户数据存储在客户端上的方式,可以利用cookie,session等跟服务器端进行数据交互 一.cookie和session Cookie和 session都是用来跟踪浏览 ...