[LeetCode]83. Remove Duplicates from Sorted List
来源:互联网 发布:坏坏的女人有魅力知乎 编辑:程序博客网 时间:2024/06/06 00:27
[LeetCode]83. Remove Duplicates from Sorted List
题目描述
思路
遍历比较,删除值相同的节点即可
代码
#include <iostream>#include <vector>using namespace std;struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {}};class Solution {public: ListNode* deleteDuplicates(ListNode* head) { if (head == NULL || head->next == NULL) return head; ListNode* cur = head; while (cur) { if (cur->next && cur->val == cur->next->val) { cur->next = cur->next->next; } else { cur = cur->next; } } return head; } void printList(ListNode* head) { while (head) { cout << head->val << " "; head = head->next; } cout << endl; } ListNode* vector2list(vector<int> nums) { if (nums.size() == 0) return NULL; ListNode* head = new ListNode(nums[0]), *iter = head; for (int i = 1; i < nums.size(); ++i) { ListNode* temp = new ListNode(nums[i]); iter->next = temp; iter = iter->next; } return head; }};int main() { vector<int> nums = { 1,1,1,1,1 }; Solution s; ListNode* head = s.vector2list(nums); s.printList(head); s.printList(s.deleteDuplicates(head)); system("pause"); return 0;}
0 0
- [LeetCode]83.Remove Duplicates from Sorted List
- [Leetcode] 83. Remove Duplicates from Sorted List
- LeetCode --- 83. Remove Duplicates from Sorted List
- [leetcode] 83.Remove Duplicates from Sorted List
- LeetCode 83. Remove Duplicates from Sorted List
- 【leetcode】83. Remove Duplicates from Sorted List
- 83. Remove Duplicates from Sorted List LeetCode
- leetcode 83. Remove Duplicates from Sorted List
- [LeetCode]83. Remove Duplicates from Sorted List
- 【LeetCode】83. Remove Duplicates from Sorted List
- LeetCode 83. Remove Duplicates from Sorted List
- [leetcode] 83. Remove Duplicates from Sorted List
- leetcode 83. Remove Duplicates from Sorted List
- LeetCode *** 83. Remove Duplicates from Sorted List
- LeetCode - 83. Remove Duplicates from Sorted List
- 【LeetCode】83. Remove Duplicates from Sorted List
- LeetCode 83. Remove Duplicates from Sorted List
- [LeetCode]83. Remove Duplicates from Sorted List
- jdk1.8中ConcurrentHashMap的实现原理
- S
- c语言链表
- 自适应手机宽度
- 我的GIS入门学习之路(一)
- [LeetCode]83. Remove Duplicates from Sorted List
- BZOJ 1045 [HAOI2008] 糖果传递
- 读书笔记《Effective C++》条款05:了解C++默默编写并调用哪些函数
- java复习——值传递机制
- Harris角点检测
- ECMAScript垃圾回收机制及性能提升
- Snackbar、CoordinatorLayout、CardView、AppBarLayout
- 2017最新python教程老男孩Python14期视频教程下载
- layer 动态加载select 失效