leetcode做题总结,题目Remove Duplicates from Sorted ListI/II 2012/04/22

本文介绍了一种链表数据结构中去除重复元素的算法实现,包括两种情况:一种是只去除相邻的重复元素,另一种是去除所有重复元素,无论它们是否相邻。提供了详细的Java代码示例,帮助读者理解算法的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一题是吧1-1-2-3-3-4变成1-2-3-4,第二题是把它变成2-4。

第一题从开头节点开始,如果下一个节点和它一样就删除,如果不一样就前进一位。

第二题需要在链表前面补一个节点,用head.next作开头进行判断,同时用tmp引用找出相同节点段的末尾一次全删掉。


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

第二题:

public ListNode deleteDuplicates(ListNode head) {
        if(head==null)return null;
        ListNode h = new ListNode(0);
        h.next=head;
        head=h;
        while(h.next.next!=null){
            ListNode tmp=h.next.next;
            if(h.next.val!=tmp.val){
                h=h.next;
                continue;
            }
            while(h.next.val==tmp.val){
                tmp=tmp.next;
                
                if(tmp==null){
                    h.next=null;
                    return head.next;
                    
                }
            }
            h.next=tmp;
            
            if(h.next==null)break;
        }
        return head.next;
    }

Update 2015/07/26:

1.

/**
 * Definition for ListNode
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param ListNode head is the head of the linked list
     * @return: ListNode head of linked list
     */
    public static ListNode deleteDuplicates(ListNode head) { 
        // write your code here
        if (head == null)
            return null;
        
        
        ListNode start = head;
        while (head.next != null){
            if (head.next.val != head.val){
                head = head.next;
            }else{
                ListNode p = head.next;
                while(p != null && p.val == head.val){
                    p=p.next;
                }
                if (p == null){
                        head.next = null;
                        return start;
                    }else{
                        head.next = p;
                        head = p;
                    }
                
            }
        }
        return start;
    }  
}

2.

/**
 * Definition for ListNode
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param ListNode head is the head of the linked list
     * @return: ListNode head of linked list
     */
    public static ListNode deleteDuplicates(ListNode head) { 
        // write your code here
        if (head == null)
            return null;
        
        
        ListNode start = head;
        while (head.next != null){
            if (head.next.val != head.val){
                head = head.next;
            }else{
                ListNode p = head.next;
                while(p != null && p.val == head.val){
                    p=p.next;
                }
                if (p == null){
                        head.next = null;
                        return start;
                    }else{
                        head.next = p;
                        head = p;
                    }
                
            }
        }
        return start;
    }  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值