Remove Duplicates from Sorted List 从链表里移除重复元素 @LeetCode
来源:互联网 发布:nba2k16mc生涯数据 编辑:程序博客网 时间:2024/06/03 08:35
题目:
在已排序好的链表中,移除重复元素
思路:
用两个指针,一个base指向被比较的对象,另一个cur指向当前比较的对象。
如果cur指向的元素和base指向元素相同,则跳过重复元素,不更新base。
如果。。。。。。。。。。。。。。不同,则更新base。
cur一直都在更新。
package Level1;import Utility.ListNode;/** * Remove Duplicates from Sorted List * * Given a sorted linked list, delete all duplicates such that each element * appear only once. * * For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. */public class S83 {public static void main(String[] args) {ListNode n1 = new ListNode(1);ListNode n2 = new ListNode(1);n1.next = n2;ListNode n3 = new ListNode(2);n2.next = n3;ListNode x = deleteDuplicates(n1);x.print();}public static ListNode deleteDuplicates(ListNode head) {ListNode base = head; // base 为每次被比较的对象if(head==null || head.next == null){return head;}ListNode cur = head.next;// cur为每次去比较的对象 while(base!=null && cur!=null){ if(base.val == cur.val){// 重复了就跳过去 base.next = cur.next; }else{// 不重复就更新base base = base.next; } cur = cur.next;// 更新cur } return head; }}
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head==null || head.next==null){ return head; } ListNode p = head, q = head; while(q != null){ if(q.val == p.val){ q = q.next; }else{ p.next = q; p = q; } } p.next = q; // Take care of last element! eg 1,1 return head; }}
- Remove Duplicates from Sorted List 从链表里移除重复元素 @LeetCode
- LeetCode 83 Remove Duplicates from Sorted List(从已排序链表中移除重复元素)(Linked List)(*)
- LeetCode 82 Remove Duplicates from Sorted List II(从已排序链表中移除重复元素)(Linked List)(*)
- [C++]Remove Duplicates from Sorted List 从已排序的链表中移除重复元素
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- Leetcode 80. Remove Duplicates from Sorted Array II(从已排序数组中移除重复元素)
- (LeetCode)Remove Duplicates from Sorted List --- 链表中删除重复元素
- Remove Duplicates from Sorted List II 移除重复元素和其本身(重重)
- Remove Duplicates from Sorted Array--从有序数组中移除重复元素
- LeetCode Remove Duplicates from Sorted Array 移除有序数列的重复元素
- leetcode 4. 移除有序数组中的重复元素 Remove Duplicates from Sorted Array
- [LeetCode-26] Remove Duplicates from Sorted Array(移除数组重复元素)
- [leetcode 26]Remove Duplicates from Sorted Array——移除排序数组中重复元素
- 【leetcode 移除有序序列重复数字】Remove Duplicates from Sorted Array(List) I(II)
- Leetcode 82. Remove Duplicates from Sorted List II 移除重复 链表版-2 解题报告
- Leetcode 83. Remove Duplicates from Sorted List 移除重复 链表版 解题报告
- Remove Duplicates from Sorted Array II 从有序数组里移除重复出现元素,最多重复出现2次 @LeetCode
- MS-PE文件全解
- ssh 框架整合试例 (spring+struts2+hibernate)
- poj - 3683 - Priest John's Busiest Day(2-SAT)
- SPOJ AMR12J 题解
- 星火杯项目笔记——iOS调用有道API翻译句子、在AppDelegate里实现全局访问的sqlite实例【13-10-14】
- Remove Duplicates from Sorted List 从链表里移除重复元素 @LeetCode
- sicp习题答案和范例代码 - 第一章
- 【Trie树篇】 字典树 字符树 前缀树 你为何这么多名字
- csdn博客開始啦!:):)
- sicp习题答案和范例代码 - 第二章 2.1
- 【伸展树篇】只有神不知道的世界---单旋Splay树
- 【伸展树篇】我仍旧不记得那天参考的代码---存父节点的Splay树
- UVa 10316 - Airline Hub
- Hyper-V 虚拟机与主机共享文件