/** * 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; } }
Remove Duplicates from Sorted List @LeetCode
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
下一篇:P1020 导弹拦截
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java map循环中remove
java map循环中remove
迭代 Java 迭代器 -
【LeetCode】39. 组合总和
【LeetCode】39. 组合总和
leetcode 数据结构与算法 C++ -
LeetCode之Remove Duplicates from Sorted List
1、题目Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3.1->2->2->2 re
LeetCode Remove Duplicates from Sorted List #include d3 -
Leetcode 83:Remove Duplicates from Sorted List
Leetcode 83:Remove Duplicates from Sorted List[穿针引线]
链表 java 数据结构 leetcode 算法