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.

思路:这个题在刚開始做的时候想的有点,怎么都没办法正确解出。后面把代码所有删除重写。思路是记录当前节点p=head,然后head往下遍历,当head的值不等于head.next的值时,结束。比較p==head,相等说明没有反复,连接上。不相等说明有反复,跳过就可以。

详细代码例如以下:

/**
* 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 first = new ListNode(0);
ListNode last = first; ListNode p = head; while(head != null){
while(head.next != null){//p不动,head后移直到head.next与p不相等
if(p.val == head.next.val){
head = head.next;//相等循环
}else{
break;//不相等结束
}
}
if(p == head){//仅仅有一个
last.next = p;//加入
last = last.next;
}
p = head = head.next;//有多个则不加入
last.next = null;//去掉关联
}
return first.next;
}
}

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

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

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

  2. [LC]83题 Remove Duplicates from Sorted List(删除排序链表中的重复元素)(链表)

    ①英文题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exa ...

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

    题目描述: 中文: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2 示例 2: 输入: 1->1->2 ...

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

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

  5. [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List

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

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

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

  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#82]Remove Duplicates from Sorted Array II

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

  9. leetcode 82. Remove Duplicates from Sorted List II

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

随机推荐

  1. UITextField限制输入长度

    首先,汉字的输入时的联想词在输入到TextFiled时,并不会走 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersIn ...

  2. poj--1904--King's Quest(scc建图)

    King's Quest Time Limit: 15000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit ...

  3. 33.unique_ptr独享内存智能指针

    #include <iostream> #include <memory> #include <string> #include <vector> us ...

  4. SparkStreaming基础

    * SparkStreaming基础 打开之前构建好的Maven工程,如何构建?请参看SparkCore基础(二)的最后部分. 在SparkCore中,我们操作的数据都在RDD中,是Spark的一个抽 ...

  5. 基于opencv的手写数字字符识别

    摘要 本程序主要参照论文,<基于OpenCV的脱机手写字符识别技术>实现了,对于手写阿拉伯数字的识别工作.识别工作分为三大步骤:预处理,特征提取,分类识别.预处理过程主要找到图像的ROI部 ...

  6. 线程1—Runnable

    随便选择两个城市作为预选旅游目标.实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市.分别用Runnable接口和Thread类实 ...

  7. 使用google API之前需要對input 做什麼 安全性的處理?

    我正要使用node.js 和 google map api做一个小应用,Google MAP API的使用URL如下: https://maps.googleapis.com/maps/api/pla ...

  8. c# window服务-初学习

    window服务-初学习 一.工具: VS2015+NET Framework4.5. 二.操作: 1.新建windows服务的项目: 2.修改windows服务相关内容: 3.预览windows服务 ...

  9. 深入理解 sudo 与 su 之间的区别

    深入理解 sudo 与 su 之间的区别 作者: Himanshu Arora 译者: LCTT zhb127 在早前的一篇文章中,我们深入讨论了 sudo 命令的相关内容.同时,在该文章的末尾有提到 ...

  10. TypeError: 'dict' object is not callabled

    Traceback (most recent call last): File "/root/Desktop/JuniperBackdoor-master/censys.py", ...