[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 }

猜你喜欢

转载自www.cnblogs.com/liuliu5151/p/9227166.html