Remove Duplicates from Sorted List II
2015-07-08 22:31
405 查看
题目描述
链接地址
解法
Example
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
链接地址
解法
题目描述
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.Example
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
链接地址
http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list-ii/解法
ListNode * deleteDuplicates(ListNode *head) { // write your code here if (head == NULL) { return head; } ListNode *dummyNode = new ListNode(); dummyNode->next = head; ListNode *pre = dummyNode; ListNode *cur = pre->next; while (cur!= NULL && cur->next!= NULL) { if (cur->val == cur->next->val) { ListNode *temp = cur; while (temp != NULL && temp->next != NULL && temp->val == temp->next->val) { ListNode *del = temp->next; temp->next = del->next; delete del; } pre ->next = temp->next; cur = pre->next; delete temp; } else { pre = cur; cur = cur->next; } } ListNode *ret = dummyNode->next; delete dummyNode; return ret; }
相关文章推荐
- Lintcode解题笔记 - 岛屿的个数
- 20150708 lintcode 总结 Binary Tree Maximum Path Sum Show result *****
- 20150708 lintcode 总结 Minimum Path Sum
- 20150708 lintcode 总结 Binary Search Tree Iterator
- 20150707 lintcode 总结 Search Range in Binary Search Tree
- 20150624 lintcode 总结 binary-tree-level-order-traversal **
- 20150624 lintcode 总结 Lowest Common Ancestor
- 20150624 lintcode 总结 Balanced Binary Tree
- Lintcode(1)-两个字符串是变位词
- Lintcode(2)-比较字符串
- Lintcode(3)-字符串查找
- Lintcode(4)-乱序字符串
- Lintcode(5)-最长公共子串
- Lintcode(6)-最长公共前缀
- Lintcode(7)-删除元素
- #LintCode# Binary Search
- Nth to Last Node in List
- Partition List
- LintCode刷题之路(一)
- 爬楼梯