leetcode 83. Remove Duplicates from Sorted List
2016-02-26 17:00
363 查看
传送门
Question
Total Accepted: 102841 Total Submissions: 284006 Difficulty: Easy
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
Subscribe to see which companies asked this question
Hide Tags
Linked List
83. Remove Duplicates from Sorted List
My SubmissionsQuestion
Total Accepted: 102841 Total Submissions: 284006 Difficulty: Easy
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.
Subscribe to see which companies asked this question
Hide Tags
Linked List
/** * 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* p1 = head->next; ListNode* p2 = head; while(p1 != NULL){ if(p1->val == p2->val){ p1 = p1->next; } else{ p2->next = p1; p2 = p2->next; p2->val = p1->val; p1 = p1->next; } } p2->next = NULL; return head; } };
相关文章推荐
- 介绍自己,并介绍github注册过程和初步使用
- Android数字签名?
- 一段代码(SAX, DOM, 或…
- Manifest.xml文件中主要包括哪些信…
- View, surfaceView, GLS…
- 谈谈android大众常用的五种布局?
- android:显式intent和隐式intent…
- sleep() 和 wait() 有什么区别?
- HashMap和Hashtable的区别?
- 谈谈final, finally, fi…
- java爬虫实战(1):抓取信息门户网站中的图片及其他文件并保存至本地
- 说出ArrayList,Vector, Linke…
- JAVA:运行时异常与一般异常有何异…
- String是最基本的数据类型吗?&nbsp…
- Collection与Collections的区别?
- 初入V4L2
- 描述forward 和redirect的区别
- JAVA xml解析
- JAVA 易错知识点(1)
- 1. 垃圾回收器的基本原理是什…