LeetCode082 Remove Duplicates from Sorted List II

详细见:leetcode.com/problems/remove-duplicates-from-sorted-list-ii


Java Solution: github

package leetcode;

import tools.ListNode辅助.*;

public class P082_RemoveDuplicatesFromSortedListII {
	public static void main(String[] args) {
//		ListNode head = tools.ListNode辅助.A_一维生成器(new int[] {1, 2, 3, 3, 4, 4, 5});
		ListNode head = tools.ListNode辅助.A_一维生成器(new int[] {4});
		ListNode ans = new Solution().deleteDuplicates(head);
		tools.ListNode辅助.B_打印链表(ans);
	}
	/*
	 * 	一次AC
	 * 	之前一直晕晕沉沉,清醒之后,好好分析,真的不难。
	 * 	1 ms
	 */
	static class Solution {
	    public ListNode deleteDuplicates(ListNode head) {
	    	if (head == null || head.next == null)
	    		return head;
	    	ListNode newHead = new ListNode(head.val == 0 ? -1 : 0);
	    	newHead.next = head;
	    	ListNode pre = newHead, cur = head.next;
	    	while (cur != null) {
	    		if (pre.next.val != cur.val) {
	    			if (pre.next.next == cur) {
	    				pre = pre.next;
	    				cur = cur.next;
	    			} else {
	    				pre.next = cur;
	    			}
	    		} else {
	    			cur = cur.next;
	    		}
	    	}
	    	if (pre.next.next != null)
	    		pre.next = null;
	        return newHead.next;
	    }
	}
}


C Solution: github

/*
    url: leetcode.com/problems/remove-duplicates-from-sorted-list-ii
    AC 3ms 42.57%
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode * pln;
typedef struct ListNode sln;

struct ListNode {
    int val;
    pln next;
};

pln ln_construct(int val) {
    pln l = (pln) malloc(sizeof(sln));
    l->next = NULL;
    l->val = val;
    return l;
}

pln deleteDuplicates(pln head) {
    pln h = NULL, t = NULL, tt = head;
    int pre_val = 0, cnt = 0;
    while (tt != NULL) {
        if (h == tt) {
            pre_val = tt->val;
            cnt = 1;
            tt = tt->next;
            continue;
        }
        if (pre_val == tt->val) {
            cnt ++;
            tt = tt->next;
            continue;
        }
        if (cnt == 1) {
            if (t == NULL) {
                h = t = ln_construct(pre_val);
            } else {
                t->next = ln_construct(pre_val);
                t = t->next;
            }
        }
        cnt = 1;
        pre_val = tt->val;
        tt = tt->next;
    }
    if (cnt == 1) {
        if (t == NULL) {
            h = t = ln_construct(pre_val);
        } else {
            t->next = ln_construct(pre_val);
            t = t->next;
        }
    }
    return h;
}


Python Solution: github

#coding=utf-8

'''
    url: leetcode.com/problems/remove-duplicates-from-sorted-list-ii
    @author:     zxwtry
    @email:      zxwtry@qq.com
    @date:       2017年4月20日
    @details:    Solution: 58ms 81.52%
'''

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None
    def __repr__(self, *args, **kwargs):
        return str(self.val)

def construct(l):
    ln = len(l)
    ls = [None] * ln
    for i in range(ln-1, -1, -1):
        ls[i] = ListNode(l[i])
        if i != ln-1:
            ls[i].next = ls[i+1]
    return ls[0]
    
def print_list_node(l):
    print("==================")
    while l != None:
        print(l.val)
        l = l.next
    print("==================")
    
class Solution(object):
    def deleteDuplicates(self, h):
        """
        :type h: ListNode
        :rtype: ListNode
        """
        if h == None: return None
        t, p1, a, p2 = h, None, h, None
        while t != None:
            if p1 == None:
                p1 = t
            elif p1.val != t.val:
                if p1.next != t:
                    if p2 == None: a = t
                    else: p2.next = t
                else: p2 = p1
                p1 = t
            t = t.next
        if p1 != None and p1.next != None:
            if p2 != None: p2.next = None
            else: a = None
        return a

if __name__ == "__main__":
    #test case:
    #[1, 2, 2, 3]
    #[1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6]
    #[2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 7]
    #[1, 2, 2, 2, 2]
    #[2, 2, 3, 3, 4, 5, 5]
    #[2, 2, 3, 3, 4, 4, 5, 5]
    h = construct([2, 2, 3, 3, 4, 4, 5, 5])
    print_list_node(Solution().deleteDuplicates(h))


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值