[LeetCode][链表去重]remove duplicates from sorted list (java)

1.题目:

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

For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->2->3.

思路:这个题目由于之前看过,比较经典的链表去重;所以比较简单

1)定义一个指针指向head

2)判断head的val和head.next.val是否相等,如果相等则删除head.next结点;

代码:一开始想用递归,结果超时了,其实简单的while循环就可以;代码逻辑比较简单

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        /*ListNode cur = head;
        while(cur != null){
            while(cur.next != null && cur.val == cur.next.val){
                cur.next = cur.next.next;
            }
            cur = cur.next;
        }
        return head;*/
        
        ListNode p = head;
        while(p != null){
           while(p.next != null && p.val == p.next.val){
               p.next = p.next.next;
           } 
            p = p.next;
        }
        return head;
    }
}

2.题目:

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.

这个题目是上面题目的扩展,已排好序的链表去掉有重复值的结点,用递归解决

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        /*if(head == null || head.next == null)
            return head;
        ListNode fakehead = new ListNode(0);
        fakehead.next = head;
        
        ListNode pre = fakehead;
        ListNode cur = fakehead.next;
        ListNode post = fakehead.next.next;
    
        boolean flag = false;
        while(post != null){
            if(post.val == cur.val){ 
                flag = true;
                post = post.next;
                if(post == null){
                    pre.next = null;
                }
            }else{
                if(flag){
                    pre.next = post;
                    flag = false;
                }else{
                    pre = cur;
                }
                cur = post;
                post = post.next;
            } 
        }
        return fakehead.next;*/
        if (head == null) return null;
 
        if (head.next != null && head.val == head.next.val) {
            while (head.next != null && head.val == head.next.val) {
                head = head.next;
            }
            return deleteDuplicates(head.next);
        } else {
            head.next = deleteDuplicates(head.next);
        }
        return head;
    }
}

 

尝试别人优秀的办法,比如不需要用到递归的,如下所示,定义一个cur指针,判断是否和下一个值相等,如果相等则cur = cur.next;此方法时间效率比较高

否则

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode prehead = new ListNode(0);
        ListNode pre = prehead;
        prehead.next = head;
        while(pre.next != null){
            ListNode cur = pre.next;
            while(cur.next != null && cur.val == cur.next.val)
                cur = cur.next;
             if (cur!=pre.next)
                pre.next=cur.next;
            else
                pre=pre.next;
        }
        return prehead.next;
        
    }
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值