题目:

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.

题解:

这道题是经典的双指针问题,用两个指针一前一后指向链表。如果两个指针指向的值相等,那么就让第二个指针一直往后挪,挪到与第一个指针不同为止。然后让第一个指针的next指向第二个指针,两个指针同时往后挪,进行下面的操作。

需要注意,当list的结尾几个node是重复的时候,例如1->2->3->3,那么ptr2会指向null,需要特殊处理,令ptr1.next = null,这样list尾部就不会丢。

其他情况就不用特殊处理结尾了,因为结尾没有重复值,只须遍历就够了,不用特殊处理尾部。

代码如下:

 1     public ListNode deleteDuplicates(ListNode head) {
 2         if(head == null || head.next == null)
 3             return head;
 4         
 5         ListNode ptr1 = head;
 6         ListNode ptr2 = head.next;
 7         
 8         while(ptr2!=null){
 9             if(ptr1.val == ptr2.val){
                 ptr2 = ptr2.next;
                 if(ptr2==null)
                     ptr1.next = null;
             }else{
                 ptr1.next = ptr2;
                 ptr1 = ptr1.next;
                 ptr2 = ptr2.next;
             }
         }
 
         return head;
     }

Remove Duplicates from Sorted List leetcode java的更多相关文章

  1. Remove Duplicates From Sorted Array leetcode java

    算法描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  2. Remove Duplicates from Sorted List @LeetCode

    /** * Remove Duplicates from Sorted List * * Given a sorted linked list, delete all duplicates such ...

  3. 【Leetcode】Remove Duplicates from Sorted List in JAVA

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

  4. Remove Duplicates from Sorted Array [LeetCode]

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

  5. Remove Duplicates From Sorted Array

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

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

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

  7. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  8. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

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

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

随机推荐

  1. imageio 载入 Buffer 格式的图片

    题注:OpenCV 使用 pip install -U opencv-python 即可安装. import zipfile import imageio import cv2 # 载入压缩文件 Z ...

  2. Oracle win32_11gR2_client.zip

    先将下载下来的ZIP文件解压,并运行setup.exe文件. 第一步:选择管理员(0MB)(A),然后点击下一步 第二步:选择语言,点击下一步 第三步:选择安装的路径,然后点击下一步 第四步:执行到第 ...

  3. 【Memory】chrome调试面板

    本篇文章以chrome版本67.0.3396.99为例,介绍如何使用Chrome和DevTools查找影响页面性能的内存问题,包括内存泄漏.内存膨胀和频繁的垃圾回收. 一.参考链接 https://d ...

  4. 《jQuery基础教程》读书笔记

    最近在看<jQuery基础教程>这本书,做了点读书笔记以备回顾,不定期更新. 第一章第二章比较基础,就此略过了... 第三章 事件 jQuery中$(document).ready()与j ...

  5. DataTable,List,Dictonary互转,筛选及相关写法

    1.创建自定义DataTable  /// 创建自定义DataTable(一) 根据列名字符串数组, /// </summary> /// <param name="sLi ...

  6. 【拉格朗日插值法】【找规律】【高精度】Gym - 101156G - Non-Attacking Queens

    题意:问你n*n的国际象棋棋盘上放3个互不攻击皇后的方案数. oeis……公式见代码内 //a(n) = 5a(n - 1) - 8a(n - 2) + 14a(n - 4) - 14a(n - 5) ...

  7. 课下测试ch17&ch18

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

  8. bzoj 4033

    树形DP,dp[i][j]表示i子树中,选了j个白点,i子树中所有边的贡献. /************************************************************ ...

  9. C#高级编程9-第7章 运算符和类型强制转换

    运算符和类型强制转换 1.运算符 运算符的简化操作 条件运算符: if-else的简化操作,也称三元运算符.如果条件为真,返回一个值,为假返回另外一个值. condition?true_value:f ...

  10. spring---transaction(4)---源代码分析(事务的状态TransactionStatus)

    写在前面 TransactionStatus表示一个具体的事务状态(这里应用到了Java的一个多继承,接口允许多继承) TransactionStatus它继承了SavepointManager接口, ...