FTPrep, 83 Remove Duplicates from Sorted List
来源:互联网 发布:淘宝达人怎么收费 编辑:程序博客网 时间:2024/06/03 22:53
值得总结的就是:与array相比,要修改(在list中是断开链接操作)一个值,没有index所以不具备array的功能。
所以只能通过node的前续节点来access/getter 并且比较,然后再做断开修改的方式.
一般只要断开的,都是通过前继节点,比如说remove the last k-th node的那道题。TODO,值得总结,list的各种处理方式
代码:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode deleteDuplicates(ListNode head) { if(head==null) return head; ListNode dummy = new ListNode(-1); dummy.next=head; ListNode checkValidForNext=dummy; ListNode scanCurrAndCompareNext=head; while(scanCurrAndCompareNext!=null){ while(scanCurrAndCompareNext.next!=null && checkValidForNext.next.val==scanCurrAndCompareNext.next.val) { scanCurrAndCompareNext=scanCurrAndCompareNext.next; } checkValidForNext.next =scanCurrAndCompareNext; // nice, directly link to the last one in any duplicate series. checkValidForNext=checkValidForNext.next; // move 1 step so points to the valid node, and ready to check the next one scanCurrAndCompareNext=scanCurrAndCompareNext.next; // move 1 step for scan pointer too. } return dummy.next; }}// 值得总结的就是:与array相比,要修改(在list中是断开链接操作)一个值,没有index所以不具备array的功能。// 所以只能通过node的前续节点来access/getter 并且比较,然后再做断开修改的方式.// 一般只要断开的,都是通过前继节点,比如说remove the last k-th node的那道题。TODO,值得总结,list的各种处理方式
阅读全文
0 0
- FTPrep, 83 Remove Duplicates from Sorted List
- FTPrep, 82 Remove Duplicates from Sorted List II
- FTPrep, 26 Remove Duplicates from Sorted Array
- LeetCode(83)Remove Duplicates from Sorted List
- [leetcode 83]Remove Duplicates from Sorted List
- [leetcode]83 Remove Duplicates from Sorted List
- [leetcode 83]Remove Duplicates from Sorted List
- leetcode-83 Remove Duplicates from Sorted List
- leetcode || 83、Remove Duplicates from Sorted List
- LeetCode 83 Remove Duplicates from Sorted List
- LeetCode---(83) Remove Duplicates from Sorted List
- leetcode 83 Remove Duplicates from Sorted List
- #83 Remove Duplicates from Sorted List
- 83Remove Duplicates from Sorted List
- LeetCode 83:Remove Duplicates from Sorted List
- leetcode[83]:Remove Duplicates from Sorted List
- Leetcode[83]-Remove Duplicates from Sorted List
- Leetcode #83 Remove Duplicates from Sorted List
- JavaMail邮件发送功能(包含多收件人 多抄送人 多附件)
- 枚举根据ordinal值获取对应文本
- Resource操作资源
- [性能] abap运行时间分析工具se30(SAT)使用介绍
- Spring Boot导入XML配置【从零开始学Spring Boot】
- FTPrep, 83 Remove Duplicates from Sorted List
- 关于chrome扩展的东西
- Linux6安装
- Eclipse常用快捷键
- Mysql 删除包含或者不包含某字符的记录
- 购物车2
- 查看SqlServer2000的外键列表
- APK重新签名
- JS实现iframe自适应高度的方法(兼容IE与FireFox)