【LeetCode】Remove Duplicates from Sorted List I && II

133 篇文章 0 订阅
121 篇文章 2 订阅

I、每个数据只允许出现1次
     Remove Duplicates from Sorted List 
     Total Accepted: 7120 Total Submissions: 20880 My Submissions
     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.
II、每个数据最多允许出现2次,注意这里是留下仅出现一次的数据
      Remove Duplicates from Sorted List II 
      Total Accepted: 4962 Total Submissions: 20108 My Submissions
      Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
      For example,
      Given 1->2->3->3->4->4->5, return 1->2->5.
      Given 1->1->1->2->3, return 2->3.
         其实和LeetCode/Remove Duplicates from Sorted Array I && II 一样,不同的只是,一个是数组,一个是链表。
         处理过程也基本类似。
         I、声明初始数据,int start = head.val; 然后循环head,依次比较数组,如果和start不相等,就把当前的val赋值给另外一个链表,
         II、这个也是判断head.val出现的次数。
        1、使用了map,扫描一遍链表,统计每个数字出现的次数。这里没有使用array[head.val]++,是因为head.val有可能为负值。第二次扫描的时候,判断当前的head.val出现了几次,如果是一次,就赋值给新链表。
         2、延续了Remove Duplicates from Sorted Array II的办法。有点复杂,而且速度也没有第一种快,也难理解。仍然是int start = head.val,然后每次扫描的时候,判断head.val和start是否相等,如果不相等,需要判断start出现几次。
         其实也就是每次给新链表赋值的时候,都是start。建议理解第一种方法。
I、Java AC

/**
 * 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){
            return null;
        }
        ListNode node = new ListNode(head.val);
        ListNode point = node;
        int start = head.val;
        head = head.next;
        while(head != null){
            if(head.val != start){
                start = head.val;
                point.next = new ListNode(head.val);
                point = point.next;
            }
            head = head.next;
        }
        return node;
    }
}

II、Java AC(1,建议采纳)

/**
 * 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){
            return null;
        }
        Map<Integer,Integer> numMap = new HashMap<Integer,Integer>();
        ListNode point = head;
        while(point != null){
            int val = point.val;
            int num = 1;
            if(numMap.containsKey(val)){
                num += numMap.get(val);
            }
            numMap.put(val,num);
            point = point.next;
        }
        ListNode node = new ListNode(0);
        point = node;
        while(head != null){
            int val = head.val;
            int num = numMap.get(val);
            if(num == 1){
                point.next = new ListNode(val);
                point = point.next;
            }
            head = head.next;
        }
        return node.next;
    }
}

II、Java AC(2,难理解)

/**
 * 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){
            return null;
        }
        Map<Integer,Integer> numMap = new HashMap<Integer,Integer>();
        ListNode point = head;
        while(point != null){
            int val = point.val;
            int num = 1;
            if(numMap.containsKey(val)){
                num += numMap.get(val);
            }
            numMap.put(val,num);
            point = point.next;
        }
        ListNode node = new ListNode(0);
        point = node;
        while(head != null){
            int val = head.val;
            int num = numMap.get(val);
            if(num == 1){
                point.next = new ListNode(val);
                point = point.next;
            }
            head = head.next;
        }
        return node.next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值