LeetCode Remove Duplicates from Sorted List
来源:互联网 发布:类似虚拟机的软件 编辑:程序博客网 时间:2024/05/16 07:09
Remove Duplicates from Sorted List
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
.
本题考点:
1 指针的熟练使用
2 判断重复元素
和Array的思想是一样的,不过对指针的操作增加了难度。
还是需要仔细画链表,一步一步地操作,那么这样的题目还是非常简单的。
class Solution {public:ListNode *deleteDuplicates(ListNode *head) {ListNode *cur = head;while (cur && cur->next){if (cur->val == cur->next->val) cur->next = cur->next->next;else cur = cur->next;}return head;}};
或者下面程序,效率是一样的,减少了插入操作:
ListNode *deleteDuplicates(ListNode *head) {if (!head) return head;ListNode *cur = head;ListNode *post = head->next;while (cur && post){while (post && cur->val == post->val) post = post->next;cur->next = post;cur = cur->next;if(post) post=post->next;}return head;}
2 0
- Remove Duplicates from Sorted List :from LeetCode
- Leetcode - List - Remove Duplicates from Sorted List
- [leetcode][list] Remove Duplicates from Sorted List
- [leetcode][list] Remove Duplicates from Sorted List
- LeetCode: Remove Duplicates from Sorted List
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- leetcode 37: Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List
- linux下进程的最大线程数、进程最大数、进程打开的文件数
- eclipse创建web工程,工程名出现红色感叹号
- ebs模拟登陆
- Deep Learning and Shallow Learning
- Cracking the coding interview--Q3.3
- LeetCode Remove Duplicates from Sorted List
- java基础知识12-javaUI1
- INT9h 中断例程的安装
- XML基础<第一篇>
- 黑马程序员--API常见对象的总结
- homebrew
- ebs r12国家表
- PE文件格式
- Linux Shell笔记之gawk