LeetCode082 Remove Duplicates from Sorted List II
来源:互联网 发布:淘宝有ego女装官网吗? 编辑:程序博客网 时间:2024/05/18 00:53
详细见: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 aif __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
- LeetCode082 Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove duplicates from sorted list II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- js实现空心正方形边框循环高亮显示
- Shader编程学习笔记(四)—— Unity Shader的组织形式(ShaderLab)
- 集合框架-List集合
- MATLAB上实现仿射变换
- Redis集群技术及Codis实践
- LeetCode082 Remove Duplicates from Sorted List II
- 台电X80HD 找不到进入Android系统的按钮
- Gym
- java中Map,List与Set的区别
- LeetCode083 Remove Duplicates from Sorted List
- linux下的find文件查找命令与grep文件内容查找命令
- ASP.NET中进行消息处理(MSMQ) 三
- Java三大核心集合类详解
- IDEA Artifacts配置说明