码迷,mamicode.com
首页 > 其他好文 > 详细

LC_83. Remove Duplicates from Sorted List

时间:2018-02-23 10:58:50      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:esc   link   pos   bsp   体会   remove   span   list   2-2   

https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/
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.

 1 public ListNode deleteDuplicates(ListNode head) {
 2         if (head == null || head.next == null) return head ;
 3         ListNode curr = head ;
 4         /*
 5         * 重点体会跳过去是什么意思 CURR =HEAD 然后改变 CURR.NEXT HEAD.next 也是会被变化的
 6         * */
 7         /*
 8         *  1-1-1-1-2-2-3
 9         *  c--->
10         *   ---->
11         *   ------->
12         *          c
13         *  h------->
14         * */
15         while(curr.next!=null){
16             if (curr.val == curr.next.val){
17                 curr.next = curr.next.next ;
18             } else {
19                 curr = curr.next ;
20             }
21         }
22         return head ;
23     }

 

LC_83. Remove Duplicates from Sorted List

标签:esc   link   pos   bsp   体会   remove   span   list   2-2   

原文地址:https://www.cnblogs.com/davidnyc/p/8460693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!