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
.
思路:这个题在刚開始做的时候想的有点,怎么都没办法正确解出。后面把代码所有删除重写。思路是记录当前节点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) 解题思路和方法的更多相关文章
- leetCode 83.Remove Duplicates from Sorted List(删除排序链表的反复) 解题思路和方法
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LC]83题 Remove Duplicates from Sorted List(删除排序链表中的重复元素)(链表)
①英文题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exa ...
- 力扣—Remove Duplicates from Sorted List(删除排序链表中的重复元素)python实现
题目描述: 中文: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2 示例 2: 输入: 1->1->2 ...
- [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 ...
- [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 ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- [LeetCode#82]Remove Duplicates from Sorted Array II
Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...
- leetcode 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- NUTCH2.3 hadoop2.7.1 hbase1.0.1.1 solr5.2.1部署(一)
Precondition: hadoop 2.7.1 Nutch 2.3 hbase 1.0.1.1 / hbase 0.98.13 solr 4.8.1 Linux version 3.16. ...
- 《鸟哥的Linux私房菜-基础学习篇(第三版)》(三)
第2章 Linxu怎样学习 1. Linux当前的应用角色 当前的Linux常见的应用可略分为企业应用和个人应用双方面. 首先谈了企业环境的利用. 1)网络server. 2)关键任务 ...
- angularjs 路由 ngRoute tab切换
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- codeforces#281 A
脑子有点秀逗 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm ...
- 详细图解mongodb下载、安装、配置与使用
记得在管理员模式下运行CMD,否则服务将启动失败 转载:http://blog.csdn.net/boby16/article/details/51221474 详细图解,记录 win7 64 安装m ...
- String slices
String slices A segment of a string is called a slice. Selecting a slice is similar selecting a char ...
- SharePoint 学习快速导航
根据我的学习过程,会不断的增加一些学习的快速链接 . 入门篇 SharePoint入门链接,针对刚刚开始了解SharePoint 的朋友,我也是处在入门的状态,随后会慢慢的累积增加 安装 | 部署 | ...
- 关于Fragment的setUserVisibleHint() 方法和onCreateView()的执行顺序
1:setUserVisibleHint(boolean isVisibleToUser)的方法就很重要,根据方法名来看当前页面是否可见, 里面的boolean值就是判断当前页面是否可见的变量,所以大 ...
- bzoj 2456: mode 思维题 好题
题目描述: 给你一个 $n$ 个数的数列,其中某个数出现了超过 n div 2 次即众数,请你找出那个数.空间大小:1mb 题解:显然,我们是不能开任何数组的,此题专卡空间.然而我们要求的东西也十分简 ...
- Linux 中挂载 ISO 文件
在 Linux 中挂载 ISO 文件 用 mount 命令,在终端中输入如下命令即可: sudo mount -o loop filename.iso /cdrom 其中 filename.iso 是 ...