Remove Duplicates from Sorted List
来源:互联网 发布:telnet 端口发送数据 编辑:程序博客网 时间:2024/05/16 09:15
我承认我链表真是一塌糊涂……
/** * 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 == NULL || head->next == NULL) return head; ListNode *last = head, *p = head->next; last->next = NULL; while (p != NULL) { if (p->val == last->val) p = p->next; else { last->next = p; last = p; p = p->next; last->next = NULL; } } return head; }};
另贴高人解法:
/** * 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 *pre,*now; if(!head||!head->next)return head; pre=head; now=head->next; while(now) { if(pre->val==now->val) { pre->next=now->next; now=now->next; } else { pre=now; now=now->next; } } return head; }};// blog.csdn.net/havenoidea
http://blog.csdn.net/havenoidea/article/details/12883023
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove duplicates from sorted list
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List
- Lucene排序取TopN源码分析
- 【Java TCP/IP Soket】— UDP套接字的基本使用
- 1137C/C++经典程序训练7---求某个范围内的所有素数
- Openstack Nova调度器及其算法
- 内存管理与检测
- Remove Duplicates from Sorted List
- 关于button
- 基于visual Studio2013解决C语言竞赛题之1053洗牌
- notepad++ 正则表达式
- 【栈】实现高级计算器
- rmmod: chdir(/lib/modules): No such file or directory 解决方法
- Openstack之Cinder服务初探
- fusioncharts 入门系列1
- 我自己的关灯游戏代码