LeetCode Linked List Easy 83. Remove Duplicates from Sorted List

时间:2023-03-10 00:14:59
LeetCode Linked List Easy 83. Remove Duplicates from Sorted List

Description

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

Example 1:

Input: ->->
Output: ->

Example 2:

Input: ->->->->
Output: ->->

问题描述:给定一个已排序链表,移除重复元素

代码:

 public ListNode DeleteDuplicates(ListNode head) {
ListNode l = head;
while(l != null && l.next != null){ if(l.val == l.next.val){
l.next = l.next.next;
}else{
l = l.next;
}
}
return head;
}

LeetCode Linked List Easy 83. Remove Duplicates from Sorted List