remove-duplicates-from-sorted-list
来源:互联网 发布:阿里云深圳机房ip地址 编辑:程序博客网 时间:2024/06/03 22:50
题目描述
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->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==NULL||head->next==NULL) return head; ListNode* pre=head; ListNode* cur=head->next; int flag=1; while(cur!=NULL) { if(pre->val==cur->val){ //相同指向后一个元素 if(pre->next!=NULL) delete pre->next; //释放删除节点的内存 pre->next=cur->next; cur=cur->next; } else{ if(flag){ //用flag记录下头部元素 head=pre;flag=0; } pre=cur; cur=cur->next; } } return head; }};
0 0
- 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
- MDK中问题:warning : type qualifier is meaningless on cast type return 的解决
- jdbc连接,方便自己查阅
- 【Android】去除小数点后多余的零
- 新手老手一起来!AngularJS 第二讲-深入基本语法
- hdu 2048 数塔 (最经典也是最简单的dp)
- remove-duplicates-from-sorted-list
- Sony S1512S2C加装内存
- linux目录权限与文件权限的区别
- CCF201412-2 Z字形扫描(解法二)(100分)
- angular js 入门2 $watch 监听事件
- 十大编程算法助程序员走上高手之路
- 【Android】退出多个Activity方法(二)
- Common Subsequence(dp)HDU 1159
- pug模板初体验