1. 程式人生 > >LeetCode Linked List Easy 83. Remove Duplicates from Sorted List

LeetCode Linked List Easy 83. Remove Duplicates from Sorted List

pro example 給定 https image ica com 描述 問題

Description

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

問題描述:給定一個已排序鏈表,移除重復元素

代碼:

 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