[leetcode]83. Remove Duplicates from Sorted List有序链表去重

Posted 程序媛詹妮弗

tags:

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

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

 

题意:

有序链表去重

 

思路:

 

 

 

 

代码:

 1 class Solution {
 2     public ListNode deleteDuplicates(ListNode head) {
 3         if(head == null) return head;       
 4         ListNode cur = head;      
 5         while(cur.next != null){
 6             if(cur.val == cur.next.val){
 7                 cur.next = cur.next.next;
 8             }else{
 9                 cur = cur.next;
10             }
11         }
12         return head;
13     }
14 }

 

以上是关于[leetcode]83. Remove Duplicates from Sorted List有序链表去重的主要内容,如果未能解决你的问题,请参考以下文章

leetcode83-Remove Duplicates from Sorted List

[LeetCode] 83. Remove Duplicates from Sorted List

LeetCode83 Remove Duplicates from Sorted List

LeetCode 83 Remove Duplicates from Sorted List

LeetCode:83.Remove Duplicates from Sorted List

leetcode83 Remove Duplicates from Sorted List