[leetcode] 82. Remove Duplicates from Sorted List II
2016-03-13 19:08
295 查看
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given
Given
这道题和第83题(传送门)相关,不同的地方是这道题中重复值的节点全部删除,而83题留下一个。题目难度为Medium。
下面介绍的方法略复杂,可以直接看最后的方法。用三个指针来辅助删除操作,curr表示当前节点,prev表示上一个节点,last表示上一个不重复值的节点,用isSameVal表示上一个节点是否是重复值节点。遍历链表,如果当前节点值和上一个节点值相等,置isSameVal为true;否则根据isSameVal进行判断,如果isSameVal为false,表明上一个节点不是重复值的节点,这样我们将last的next指针指向prev,如果last此时等于NULL,表明之前还没有不重复值的节点出现,所以要把head指针指向prev,之后更新last指针为prev,同时将isSameVal标志置为false。遍历完链表之后还要注意链表尾的处理,如果last不为NULL,根据isSameVal标志分别处理,isSameVal为true表明尾节点为重复值节点,将last的next指针置为NULL,isSameVal为false表明尾节点不是重复值节点,将last的next指针指向尾节点;如果last为NULL,同样根据isSameVal标志分别处理,isSameVal为true说明整个链表没有不重复值的节点,直接返回NULL,isSameVal为false说明只有尾节点不重复值,返回尾节点。具体代码:class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head) return NULL;
ListNode* curr = head->next;
ListNode* prev = head;
ListNode* last = NULL;
bool isSameVal = false;
while(curr) {
if(curr->val == prev->val) {
isSameVal = true;
}
else {
if(!isSameVal) {
if(last) last->next = prev;
else head = prev;
last = prev;
}
isSameVal = false;
}
prev = curr;
curr = curr->next;
}
if(last) {
if(isSameVal) last->next = NULL;
else last->next = prev;
}
else {
if(isSameVal) return NULL;
else return prev;
}
return head;
}
};分析略显复杂,看了别人的代码,增加一个空的链表头节点,这样在遍历过程中判断会简单很多,尤其在链表头和尾的判断上简化很多,大家可以学习下。具体代码:
For example,
Given
1->2->3->3->4->4->5, return
1->2->5.
Given
1->1->1->2->3, return
2->3.
这道题和第83题(传送门)相关,不同的地方是这道题中重复值的节点全部删除,而83题留下一个。题目难度为Medium。
下面介绍的方法略复杂,可以直接看最后的方法。用三个指针来辅助删除操作,curr表示当前节点,prev表示上一个节点,last表示上一个不重复值的节点,用isSameVal表示上一个节点是否是重复值节点。遍历链表,如果当前节点值和上一个节点值相等,置isSameVal为true;否则根据isSameVal进行判断,如果isSameVal为false,表明上一个节点不是重复值的节点,这样我们将last的next指针指向prev,如果last此时等于NULL,表明之前还没有不重复值的节点出现,所以要把head指针指向prev,之后更新last指针为prev,同时将isSameVal标志置为false。遍历完链表之后还要注意链表尾的处理,如果last不为NULL,根据isSameVal标志分别处理,isSameVal为true表明尾节点为重复值节点,将last的next指针置为NULL,isSameVal为false表明尾节点不是重复值节点,将last的next指针指向尾节点;如果last为NULL,同样根据isSameVal标志分别处理,isSameVal为true说明整个链表没有不重复值的节点,直接返回NULL,isSameVal为false说明只有尾节点不重复值,返回尾节点。具体代码:class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head) return NULL;
ListNode* curr = head->next;
ListNode* prev = head;
ListNode* last = NULL;
bool isSameVal = false;
while(curr) {
if(curr->val == prev->val) {
isSameVal = true;
}
else {
if(!isSameVal) {
if(last) last->next = prev;
else head = prev;
last = prev;
}
isSameVal = false;
}
prev = curr;
curr = curr->next;
}
if(last) {
if(isSameVal) last->next = NULL;
else last->next = prev;
}
else {
if(isSameVal) return NULL;
else return prev;
}
return head;
}
};分析略显复杂,看了别人的代码,增加一个空的链表头节点,这样在遍历过程中判断会简单很多,尤其在链表头和尾的判断上简化很多,大家可以学习下。具体代码:
class Solution { public: ListNode* deleteDuplicates(ListNode* head) { ListNode fakeHead(0); fakeHead.next = head; ListNode* pre = &fakeHead; ListNode* cur = head; while(cur) { while(cur->next && cur->val==cur->next->val) { cur = cur->next; } if(pre->next == cur) pre = cur; else pre->next = cur->next; cur = cur->next; } return fakeHead.next; } };
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解