/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *deleteDuplicates(ListNode *head) { if(!head) return NULL; if(!head->next) return head; ListNode *dh = new ListNode(0); dh->next = head; ListNode *pre = dh; ListNode *last = head; ListNode *cur = head->next; int cn = 1; while(cur) { if(cur->val != last->val) { if(cn > 1) { while(last != cur) { pre->next = last->next; delete last; last = pre->next; } cn = 1; } else { pre = last; last = last->next; } } else { cn ++; } cur = cur->next; } if(cn>1) { while(last != cur) { pre->next = last->next; delete last; last = pre->next; } } cur = dh->next; delete dh; return cur; } };
相关推荐
Remove Duplicates from Sorted List II题目: | 源码:标签:单向链表难度:中等 / Medium146. LRU Cache题目: | 源码:标签:哈希表,双向链表难度:中等 / Medium212. Word-Search-II题目: | 英文站源码:./...
- **2.2.5 Remove Duplicates from Sorted List II** - 移除排序链表中的重复元素,包括头部。 - 实现思路:使用虚拟头结点简化边界条件处理。 - **2.2.6 Rotate List** - 旋转链表。 - 实现思路:先找到链表...
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack ...Duplicates from Sorted List 141 * Linked List Cycle 160 * Intersection of Two Linke
leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack ...Duplicates from Sorted List 141 * Linked List Cycle 160 * Intersection of Two Linke
II 和 Find Minimum in Rotated Sorted Array这两道题目,我也是醉了。。。代码完全一样的不说,题目还都特别傻逼。 Maximum Product Subarray 动态规划,类似于求最大连续和,但这里由于是乘法,还要考虑符号问题。...
**1.6 Remove Duplicates from Sorted List II (82)** - **问题描述**:给定一个已排序的链表,删除其中所有重复的元素,使得每个元素只出现一次。 - **解题思路**: - 使用虚拟头节点帮助处理边界情况。 - 遍历...
LeetCode 原创文章每周最少两篇,后续最新文章会在首发,视频首发,大家可以加我进交流群,技术交流或提意见都可以,欢迎Star! 帮助文档 帮助文档存放在Help文件夹下。...Remove Duplicates from Sorted Lis
26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates...
83-删除排序链表中的重复元素:remove-duplicates-from-sorted-list 92-反转链表II:reverse-linked-listt-ii 141-环形链表:linked-list-cycle 142-环形链表:linked-list-cycle-ii 160-相交链表:intersection-of-two-...
1. **去除排序数组中的重复项(Remove Duplicates from Sorted Array)** - **问题描述**:给定一个排序数组,在原地删除重复出现的元素,使得每个元素只出现一次,并返回新的长度。不使用额外数组空间。 - **解决...
13)..Added: "User" and "Session" columns to processes list, processes list is also sorted by session first 14)..Added: Support for showing current user processes only 15)..Added: Expanding environment...