LC_83. Remove Duplicates from Sorted List

Posted davidnyc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LC_83. Remove Duplicates from Sorted List相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted Listeasy

83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List