您的位置:首页 > 其它

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

2014-01-22 16:26 381 查看
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;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: