【Leetcode】Remove Duplicates from Sorted List
2014-03-11 19:09
288 查看
题目:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
解题思路:设置两个指针CurrList和NewList,初始化为同一头指针,CurrList不断向链表尾部扫描,如果当前节点的值不是重复值,就将该节点接到NewList尾部。
代码:
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.
解题思路:设置两个指针CurrList和NewList,初始化为同一头指针,CurrList不断向链表尾部扫描,如果当前节点的值不是重复值,就将该节点接到NewList尾部。
代码:
/** * 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){ return NULL; } int temp=head->val; ListNode *CurrList=head; ListNode *NewList=head; while(CurrList->next){ CurrList=CurrList->next; if(CurrList->val!=temp){ NewList->next=CurrList; NewList=NewList->next; temp=CurrList->val; } } NewList->next=CurrList->next; return head; } };
相关文章推荐
- 十大OpenGL教程
- 黑马程序员 第3天 JAVA if和switch语句
- Java修改properties文件
- 【Kettle从零开始】实例下载
- ubuntu进程管理方法
- 分布式应用通信协议
- LeetCode Minimum Depth of Binary Tree
- JS常用的正则表达式总结
- Apache mod_wsgi部署Django项目
- BINARY SEARCH in read table statement
- Linux下chkconfig用法详解
- Nginx server之Nginx作为web服务器(基于域名或IP的web服务器)
- MVC中JQuery文件引入的路径问题,@Url.Content函数
- [问题2014S01] 解答
- html5的Input Attr : Autofocus,感觉挺有意思的!
- 关键词提取1-C#
- RSA算法原理
- 排序之选择排序
- intent 分析,显式隐式
- nagios监控服务器