leecode 解题总结:83. Remove Duplicates from Sorted List
2017-02-13 00:02
387 查看
#include <iostream> #include <stdio.h> #include <vector> using namespace std; /* 问题: Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 分析:删除链表中的冗余元素,多个元素只保留一个。 我们保留多个重复元素中最后一个节点。 同时设定当前节点和下一个节点。如果当前节点和下一个节点相同,说明 当前元素是重复的,记录当前元素之前的节点previoudNode,然后一直寻找到和当前元素不同的节点,记为nextNode,则nextNode前面一个节点 就是需要保留的节点node,然后设定previouNode指向node即可 输入: 3(结点个数) 1 1 2 4 1 1 2 2 2 1 1 5 1 1 2 3 3 输出: 1 2 1 2 1 1 2 3 关键: 1】 1,1,2,2报错了 //删除完之后,进行连接 if(previous) { previous->next = node; } //删除头部节点,则重新设置链表头部 else { head = node; } //这里需要更新previous为当前node(保留的重复元素的最后一个节点),否则会出现错误, 记住previous记录的就是与下一个元素不重复的节点 previous = node; */ struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { public: ListNode* deleteDuplicates(ListNode* head) { if(!head) { return head; } //只有一个元素直接返回 if(!(head->next)) { return head; } ListNode* previous = NULL; ListNode* node = head; ListNode* next = head->next; ListNode* tempNode; ListNode* deleteNode; int repeatValue; int countRepeat = 0; while(next) { //判断当前元素和下一个元素是否重复 if(node->val == next->val) { //寻找第一个不重复的元素 repeatValue = node->val; countRepeat = 0; while(next && next->val == repeatValue ) { node = next; next = next->next; countRepeat++; } //此时node是最后一个重复的元素,删除重复元素 if(previous) { deleteNode = previous->next; } //说明链表头部就重复了 else { deleteNode = head; } while(deleteNode && countRepeat) { tempNode = deleteNode->next; delete deleteNode; deleteNode = tempNode; countRepeat--; } //删除完之后,进行连接 if(previous) { previous->next = node; } //删除头部节点,则重新设置链表头部 else { head = node; } //这里需要更新previous为当前node(保留的重复元素的最后一个节点),否则会出现错误 previous = node; } else { previous = node; } node = next; if(next) { next = next->next; } } return head; } }; void print(ListNode* head) { if(!head) { cout << "no result" << endl; } ListNode* tempHead = head; while(head) { cout << head->val << " "; head = head->next; } cout << endl; head = tempHead; } ListNode* buildList(vector<int>& nums) { if(nums.empty()) { return NULL; } int size = nums.size(); ListNode* head ; ListNode *tail; ListNode* node; for(int i = 0 ; i < size ; i++) { if(i) { node = new ListNode(nums.at(i)); tail->next = node; tail = node; } else { head = new ListNode(nums.at(i)); tail = head; } } return head; } void deleteList(ListNode* head) { ListNode* node; while(head) { node = head->next; delete head; head = node; } } void process() { vector<int> nums; int value; int num; Solution solution; vector<int> result; while(cin >> num ) { nums.clear(); for(int i = 0 ; i < num ; i++) { cin >> value; nums.push_back(value); } ListNode* head = buildList(nums); ListNode* newHead = solution.deleteDuplicates(head); print(newHead); deleteList(newHead);//删除节点了 } } int main(int argc , char* argv[]) { process(); getchar(); return 0; }
相关文章推荐
- leecode 解题总结:80. Remove Duplicates from Sorted Array II
- leecode 解题总结:26 Remove Duplicates from Sorted Array
- Leecode Remove Duplicates from Sorted List
- leecode 解题总结:19 Remove Nth Node From End of List
- CODE 51: Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- leetcode解题方案--083--Remove Duplicates from Sorted List
- Leetcode:83. Remove Duplicates from Sorted List(JAVA)
- 83. Remove Duplicates from Sorted List
- [LeetCode]Remove Duplicates from Sorted List解题报告
- LeetCode:83.Remove Duplicates from Sorted List
- leecode 解题总结:109. Convert Sorted List to Binary Search Tree
- leetcode解题方案--082--Remove Duplicates from Sorted ListII
- CODE 50: Remove Duplicates from Sorted List II
- 83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List (链表)
- 83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List
- Easy-题目17:83. Remove Duplicates from Sorted List