Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.
 1 public class Solution {
 2     public ListNode deleteDuplicates(ListNode head) {
 3         ListNode cur =head;
 4         while(cur!=null&&cur.next!=null){
 5             if(cur.val==cur.next.val){
 6                 cur.next = cur.next.next;
 7             }
 8             else{
 9                 cur = cur.next;
10             }
11         }
12         return head;
13     }
14 }
View Code

 

posted @ 2014-02-06 14:42  krunning  阅读(80)  评论(0编辑  收藏  举报