Leetcode Problem.83—Remove Duplicates from Sorted List

来源:互联网 发布:淘宝发票抬头设置 编辑:程序博客网 时间:2024/06/11 20:36

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.

My C++ solution!

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */

ListNode* deleteDuplicates(ListNode* head) {      vector<int> temp;  if(head==NULL)  return head;  ListNode *p=head;  ListNode *q;  temp.push_back(p->val);  while(p->next)  {  if(find(temp.begin(),temp.end(),p->next->val)!=temp.end())  { q =p->next->next; p->next=q;  }  else  {  temp.push_back(p->next->val);  p=p->next;  }  }  return head;}


0 0
原创粉丝点击