LeetCode: Remove Duplicates from Sorted List
来源:互联网 发布:unity3d喷泉粒子特效 编辑:程序博客网 时间:2024/06/08 19:02
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) { ListNode *cur = head, *iter = NULL; while(cur != NULL) { iter = cur; while(iter->next != NULL && iter->val == iter->next->val) { iter = iter->next; } cur->next = iter->next; cur = cur->next; } return head; }};
Round 2:
class Solution {public: ListNode *deleteDuplicates(ListNode *head) { ListNode *cur = head, *iter = NULL; while(cur != NULL) { iter = cur; while(iter->next != NULL && iter->val == iter->next->val) { iter = iter->next; } cur->next = iter->next; cur = cur->next; } return head; }};
Round 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) { ListNode *iter = head; while(iter != NULL) { if(iter->next == NULL) break; if(iter->val == iter->next->val) { iter->next = iter->next->next; } else { iter = iter->next; } } return head; }};
0 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
- 匹配 Hdu1151
- Spring官方文档翻译——15.1 介绍Spring Web MVC框架
- linux入门详细介绍
- 公司实习经验分享
- 玩具javascript:隐藏table指定列
- LeetCode: Remove Duplicates from Sorted List
- 伪代码:括号匹配
- JAVA项目中发布WebService服务——简单实例
- 【Android】避免图片等资源泄露在系统图库当中
- Custom iphone cases gGgn rITx 7bizM
- HDOJ题目2660 Accepted Necklace(二维01背包)
- fut 14 coins Fa6F KleS cwOwi
- fifa coins R63p 5FJJ ImQNV
- ZipUtil