码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]: 83: Remove Duplicates from Sorted List

时间:2015-10-06 10:22:48      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

题目:

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

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

 

思路与分析:直接遍历

 

代码:

    public static ListNode deleteDuplicates(ListNode head) {
        if(head == null){
            return null;
        }
        
        int intFlag = head.val;
        //ListNode listCurrent = head.next;
        ListNode listCurrent = head;
        
        
        while(listCurrent.next != null){
            if(intFlag == listCurrent.next.val){
                if(listCurrent.next.next != null){
                    listCurrent.next = listCurrent.next.next;
                }
                else{
                    listCurrent.next = null;
                }
            }
            else{
                intFlag = listCurrent.next.val;
                listCurrent = listCurrent.next;
            }
            
            
        }

        return head;
    }

 

[LeetCode]: 83: Remove Duplicates from Sorted List

标签:

原文地址:http://www.cnblogs.com/savageclc26/p/4856705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!