LeetCode 82 Remove Duplicates from Sorted List II(从已排序链表中移除重复元素)(Linked List)(*)
来源:互联网 发布:ubc专业 知乎 编辑:程序博客网 时间:2024/05/20 19:46
翻译
给定一个已排序链表,删除所有的重复节点,只保留原始链表中独特的数字。
例如,
给定 1->2->3->3->4->4->5, 返回 1->2->5.
给定 1->1->1->2->3, 返回 2->3.
原文
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.
分析
这类问题一般都要特地构造一个头部,比如说
1 -> 1 -> 2 -> 3 -> NULL
这个时候如果把1删掉了,那么head放在哪?相反,如果是
x -> 1 -> 1 -> 3 -> NULL
这样的话,把1都删掉了,x还可以充当头部,不管取什么值都可以,反正最后返回的肯定是
return x->next;
不知道我排版得有没有对齐,应该不影响阅读吧。反正核心思想是,如果p所在的位置和下一个位置的值相同,就一直往后走。而且走到最后一个3的时候,记得还要再走一步才能到4。
那么c和p之间如何跨越这么大的鸿沟呢,最后一句c->next = p,就将它们有紧密的联合起来了,绝对是一对好CP,这也是我给它们这样命名的原因。
之所以要设定这么一个CP,就是因为在这里不像上一题一样,删除重复节点的时候是都要删掉,不会留下一个。换句话说,如果是每次只删一个,最后肯定会有一个留下没有被删除,因为它前后重复都删掉了,剩下继续比较的都是不重复的。因此应该加一个标记,用于最后一次性删除。这里的删除也就是跳过一个节点。
LeetCode 83 Remove Duplicates from Sorted List(从已排序链表中移除重复元素)(Linked List)
代码
C Plus Plus
ListNode *deleteDuplicates(ListNode *head) { if (head == NULL || head->next == NULL) return head; ListNode *newHead = new ListNode(0); newHead->next = head; ListNode *c = newHead, *p = head; while (p != NULL && p->next != NULL) { if (p->val != p->next->val) { c = c->next; p = p->next; } else { while (p->next != NULL && p->val == p->next->val) { p = p->next; } p = p->next; c->next = p; } } return newHead->next;}
做一个小幅修改,看上去更加科学一点。
updated at 2016/08/14
ListNode* deleteDuplicates(ListNode *head) { if (!head || !head->next) return head; ListNode *newHead = new ListNode(0); newHead->next = head; ListNode *c = newHead, *p = c->next; while (c->next && c->next->next) { if (c->next->val == c->next->next->val) { p = p->next; while (p->next && p->val == p->next->val) { p = p->next; } c->next = p->next; } else { c = c->next; p = p->next; } } return newHead->next;}
Java
updated at 2016/09/23
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null || head.next == null) return head; ListNode newHead = new ListNode(0); newHead.next = head; ListNode c = newHead, p = c.next; while (c.next != null && c.next.next != null) { if (c.next.val == c.next.next.val) { p = p.next; // 标记要跳过的节点 while (p.next != null && p.val == p.next.val) { p = p.next; // 继续循环标记要删除的节点 } c.next = p.next; } else { c = c.next; p = p.next; } } return newHead.next; }}
- LeetCode 82 Remove Duplicates from Sorted List II(从已排序链表中移除重复元素)(Linked List)(*)
- LeetCode 83 Remove Duplicates from Sorted List(从已排序链表中移除重复元素)(Linked List)(*)
- Leetcode 80. Remove Duplicates from Sorted Array II(从已排序数组中移除重复元素)
- [C++]Remove Duplicates from Sorted List 从已排序的链表中移除重复元素
- Remove Duplicates from Sorted List II 移除重复元素和其本身(重重)
- Leetcode刷题记—— Remove Duplicates from Sorted Array II(已排序数组移除重复元素2)
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- 【LeetCode-面试算法经典-Java实现】【082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)】
- leetcode(83)—— 删除已排序链表重复元素(Remove Duplicates from Sorted List)
- Remove Duplicates from Sorted List 从链表里移除重复元素 @LeetCode
- leetcode系列(66)Remove Duplicates from Sorted List,Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted Linked List 从排序链表中去掉重复值
- leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的重复II) 解题思路和方法
- LeetCode[Linked List]: Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II 删除链表中的元素(不保留重复)
- LeetCode 题解(160): Remove Duplicates from Sorted List II
- Leetcode-Remove Duplicates from Sorted List II(二级指针)
- CountDownLath
- 编码练习——Java-5-类和对象
- poj 3414 Pots
- HDU 2588 GCD [欧拉函数]【数论】
- sdut oj3375 数据结构实验之查找三:树的种类统计
- LeetCode 82 Remove Duplicates from Sorted List II(从已排序链表中移除重复元素)(Linked List)(*)
- WebSocket协议深入理解
- HDU1051:Wooden Sticks
- VideoView学习笔记
- java SE基础部分 点
- ACDream 1734 Can you make a water problem?(贪心)
- LCS 及LCS+滚动数组 模板
- UVA - 705 Slash Maze
- 原型相关