LeetCode Algorithms #83 <Remove Duplicates from Sorted List>
来源:互联网 发布:菠萝彩软件下载 编辑:程序博客网 时间:2024/06/05 11:36
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
.
思路:
一个指针向后遍历,发现自己的下一个节点的值和当前指向节点的值,就暂停遍历,把下一个节点的下一个节点当做下一个节点。直到找到下一个节点不同时,再继续遍历。
解:
/** * 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 nullptr; } ListNode* stepNode = head; while(stepNode->next != nullptr) { if(stepNode->val == stepNode->next->val) { stepNode->next = stepNode->next->next; continue; } stepNode = stepNode->next; } return head; }};
0 0
- LeetCode Algorithms #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
- 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
- 滤镜模糊效果
- IntelliJ IDEA 14 编译程序出现“非法字符: \65279”
- CodeForces 55D Beautiful numbers(数位dp&&离散化)
- 关于 dismissviewcontrolleranimated 的延迟问题
- 开发的通用PHP框架,RollerPHP
- LeetCode Algorithms #83 <Remove Duplicates from Sorted List>
- .properties 文件
- 51Nod 1031 骨牌覆盖(Fibonacci 数列,有趣)
- R语言-理解R性能
- exit()那些事
- nginx架构分析
- 面向对像的C语言
- java 农历处理转换和显示输出
- select、poll和epoll的异同【整理】