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

LeetCode 83. Remove Duplicates from Sorted List

list null each ron mov lee ive div ack

分析

難度 易

來源

https://leetcode.com/problems/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 package LeetCode;
2 3 /** 4 * Definition for singly-linked list. 5 * public class ListNode { 6 * int val; 7 * ListNode next; 8 * ListNode(int x) { val = x; } 9 * } 10 */ 11 public class L83_RemoveDuplicatesFromSortedList { 12 public ListNode deleteDuplicates(ListNode head) { 13 if
(head==null) 14 return head; 15 ListNode cur=head; 16 //ListNode temp; 17 while(cur.next!=null){ 18 if(cur.val!=cur.next.val) 19 cur=cur.next; 20 else{ 21 /*temp=cur.next.next; 22 cur.next=temp;*/ 23
cur.next=cur.next.next; 24 } 25 } 26 return head; 27 } 28 }

LeetCode 83. Remove Duplicates from Sorted List