您的位置:首页 > 其它

leetcode 83. Remove Duplicates from Sorted List

2016-02-26 17:00 363 查看
传送门

83. Remove Duplicates from Sorted List

My Submissions
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
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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: