Remove Duplicates from Sorted List解题报告
---恢复内容开始---
Remove Duplicates from Sorted List
题目意图:删除掉linked list里面的重复的元素
思路: 1. 对于重复的点,会保留当前数字第一次出现的点,所以返回结果的起始点与当前起始点为同一个点,不需要新建dummy node来表示起始点。
2. 用一个pre pointer遍历整个linked list,如果当前点与下一个点一致则删除下一个点。
我的代码:
1 public static ListNode deleteDuplicates(ListNode head) { 2 // write your code here 3 if (head == null) { 4 return head; 5 } 6 7 ListNode pre = new ListNode(0); 8 pre = head; 9 while (pre != null && pre.next != null) { 10 while(pre.next != null && pre.next.val == pre.val){ 11 pre.next = pre.next.next; 12 } 13 pre = pre.next; 14 } 15 return head; 16 }
九章代码:
public ListNode deleteDuplicates(ListNode head) {if (head == null) {return null;}ListNode node = head;while (node.next != null) {if (node.val == node.next.val) {node.next = node.next.next;} else {node = node.next;}}return head;}
反思:1. 对于一维的linked list,只需要分当前点和下一个点是否相同,做一次遍历就好。
2. 对于我的方法,在内层while之行删除所有可能与当前点一致点后,把pre指针往后移。注意要判断是否已经到linked list的结尾。
Remove Duplicates from Sorted List II
1 public class Solution { 2 public static ListNode deleteDuplicates(ListNode head) { 3 if (head == null) { 4 return head; 5 } 6 ListNode dummy = new ListNode(0); 7 dummy.next = head; 8 ListNode pre = new ListNode(0); 9 pre = dummy; 10 while (head.next != null) { 11 if (head.val == head.next.val) { 12 while (head.next != null && head.val == head.next.val) { 13 head.next = head.next.next; 14 } 15 pre.next = head.next; 16 if(pre.next != null){ 17 head = pre.next; 18 } 19 } else { 20 head = head.next; 21 pre = pre.next; 22 } 23 } 24 return dummy.next; 25 } 26 }
用了dummy和pre pointer,dummy pointer用于存储整个linked list删除后的头,pre pointer用于存储内部循环被对比节点的前一个点(eg: abc 循环到b与c比较时,pre纪录a,防止b和c相同,同时被删除)。
删除时候,外部循环用于遍历整个linked list。内部循环用于对制定节点遍历后面的所有节点,如果相同删除后面的这个节点,循环中每次只删除后面的这一个节点。对于被比较的这个节点,在内部while循环结束时删除,同时pre pointer不变,更新head节点。
九章代码:
public class Solution {public ListNode deleteDuplicates(ListNode head) {if(head == null || head.next == null)return head;ListNode dummy = new ListNode(0);dummy.next = head;head = dummy;while (head.next != null && head.next.next != null) {if (head.next.val == head.next.next.val) {int val = head.next.val;while (head.next != null && head.next.val == val) {head.next = head.next.next;} } else {head = head.next;}}return dummy.next;} }
九章的代码中内部循环的方式更加简单
用head pointer 当作pre pointer,对head->next和head->next 做对比
在外部循环一旦确定2个值相等,用一个val的临时变量纪录当前的值,然后从head->next开始一个一个的向后删除。这样做避免了在内部循环时候需要一一个删除,最后删除当前被比较的node的情况。 因此在内部循环,始终只考虑是否相等,一次删除一个节点。
反思: 多重循环时候,尽量保持一重循环只做一件事情,使得代码简化。对于我的方法中的比较数和被比较数均可通过一个point的next和next next来表示,可以不用2个变量。
---恢复内容结束---
转载于:https://www.cnblogs.com/jiangchen/p/5397544.html
Remove Duplicates from Sorted List解题报告相关推荐
- LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array
LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array 问题 Given a sorted array nums, remove the du ...
- Remove Duplicates from Sorted Array II -- LeetCode
原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 这道题跟Remove Duplicates ...
- 2016.5.57—— Remove Duplicates from Sorted List
Remove Duplicates from Sorted List 本题收获: 指针: 不管什么指针在定义是就初始化:ListNode *head = NULL; 如果给head指针赋值为第一个no ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- 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++> 给出排序好的 ...
- Leetcode OJ: Remove Duplicates from Sorted Array I/II
删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...
- LeetCode26. Remove Duplicates from Sorted Array
问题链接:LeetCode26. Remove Duplicates from Sorted Array 注意点: 1.数组中可能是0个元素: 2.C++程序中,循环变量声明不能写在for语句中(编译 ...
- [leetcode]83.Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...
- 【11_83】Remove Duplicates from Sorted List
这道题本质上不难,难的是细节处理,容易出错. 第一遍写的代码越改越大,越臃肿,此时,不如推倒重写,果然,第二次一遍过. Remove Duplicates from Sorted List My Su ...
- 2016.6.17——Remove Duplicates from Sorted Array
Remove Duplicates from Sorted Array 本题收获: 1."删除"数组中元素 2.数组输出 题目: Given a sorted array, rem ...
最新文章
- php进程学习(一)
- python根据月份获取月初月末_用python获取月末数据
- eclipse 重构_Eclipse对类固醇的重构
- 本地添加Maven管理
- html新标准,HTML 5新标准将会在2022年正式发布
- 注意判断js中使用正则表达式的转义字符,到底是对谁进行转义!!!
- Django学习笔记5-url
- 《中国人工智能学会通讯》——1.20 聊天机器人研究存在的挑战
- 2015 2020 r4烧录卡 区别_行业新闻2020年中国智能门锁行业发展现状分析
- seo关键词布局,网页头部优化(TDK),网站优化(古志强网络营销师)
- sql server触发器写法
- pytohn 单下划线与双下划线的区别
- quartus .bdf格式 和 .v格式 互相转换及调用
- 京东商城手机爬虫和数据分析项目
- DPad down是哪个键_上海crm系统哪个好_仓库erp管理
- vmware 三种连接方式 如何设置独立ip
- 软件测试 | 测试开发 | 音频质量检测模型中标准数据集的构建方法
- 盘点多边形战士 Polygon 的扩容解决方案
- 餐厅(秋季每日一题 37)
- 工业视觉检测关键技术