数据结构-02day

数据结构-02day

82. 删除排序链表中的重复元素 II (leetcode)

题目链接

解题思路

暴力破解

  1. 先循环一次链表获取到重复的节点值,存入到ArryList中
  2. 再循环一次链表并循环ArrayList将有的节点一次删除

执行用时 :2 ms 内存消耗 :39.1 MB

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null) {
    		return null;
    	}
    	List<Integer> list = new ArrayList<>();
    	ListNode temp = new ListNode(0);
    	temp.next = head;
    	ListNode t = temp;
    	while(head.next != null) {
    		if(head.val == head.next.val) {
    			list.add(head.val);
    			while(head.next != null && head.val == head.next.val) {
    				head = head.next;
    			}
    		}else {
    			head = head.next;
    		}
    	}
    	
    	while(temp.next != null && !list.isEmpty()) {
    		int num = list.get(0);
    		if(temp.next.val == num) {
    			while(temp.next.val == num && temp.next.next != null) {
    				temp.next = temp.next.next;
    			}
    			if(temp.next.val == num && temp.next.next == null) {
    				temp.next = null;
    			}
    			list.remove(0);
    		}else {
    			temp = temp.next;
    		}
    	}
    	return t.next;
    }
}

暴力破解,内存和时间都消耗较大,我继续改进,也希望得到大家宝贵的意见

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值