删除有序链表中重复的元素

删除有序链表中重复的元素

http://www.nowcoder.com/questionTerminal/c087914fae584da886a0091e877f2c79

整体来说链表题都可以通过多个指针来解,或者借助其他的数据结构
这边采用双指针的思路

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        if(head==null||head.next==null){
            return head;
        }

        ListNode start=head;
        ListNode estart=head.next;
        while(start!=null&&start.next!=null){
            if(start.val==estart.val){
                while(estart!=null){
                    if(estart.val==start.val){
                        estart=estart.next;
                    }else{
                        break;
                    }
                }
                start.next=estart;
                if(estart==null){
                    return head;
                }else{
                    start=estart;
                }
            }else{
                start=start.next;
                estart=start.next;
            }
        }
        return head;

    }
}
全部评论

相关推荐

09-13 08:41
服装/纺织设计
那一天的Java_J...:你第一次参加面试吗
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务