leetcode82~Remove Duplicates from Sorted List II
来源:互联网 发布:麦吉尔大学知乎 编辑:程序博客网 时间:2024/04/30 17:14
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.
本质是三个指针,一个是记录重复数字之前的指针,一个是当前指针,一个是重复数字之后的指针
public class RemoveDuplicatesfromSortedListII { public ListNode deleteDuplicates2(ListNode head) { if(head==null || head.next==null) return head; ListNode dummyNode = new ListNode(-1); dummyNode.next = head; ListNode pre = dummyNode; ListNode cur = head; while(cur!=null) { while(cur.next!=null && cur.val==cur.next.val) { cur = cur.next; } if(pre.next==cur) { pre = pre.next; } else { pre.next = cur.next; } cur = cur.next; } return dummyNode.next; } public ListNode deleteDuplicates(ListNode head) { if(head==null || head.next==null) { return head; } ListNode dummyNode = new ListNode(-1); dummyNode.next = head; head = dummyNode; 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 dummyNode.next; }}
0 0
- LeetCode82:Remove Duplicates from Sorted List II
- leetcode82:Remove Duplicates from Sorted List II
- LeetCode82 Remove Duplicates from Sorted List II
- leetcode82. Remove Duplicates from Sorted List II
- LeetCode82.Remove Duplicates from Sorted List II
- leetcode82. Remove Duplicates from Sorted List II
- leetcode82~Remove Duplicates from Sorted List II
- LeetCode82 Remove Duplicates from Sorted List II
- LeetCode82——Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- 四.文件自校验大小破解(GetFileSize)
- PAT-A 1029. Median (25)
- GYM 101149 I.It's the Police(水~)
- codeforces 791B
- 程序小白该,如何从零开始学Java编程?
- leetcode82~Remove Duplicates from Sorted List II
- 【知识库】--mysql engines MyISAM vs InnoDB(146)
- Hello,World!
- 你好
- 愿天下有情人都是失散多年的兄妹
- : AH00436: No installed service named "Apache2.4".
- #cs231n#Assignment2:Dropout.ipynb
- 算法训练 表达式计算 中后缀表达
- 栈的压入、弹出序列