[leetcode oj 83]Remove Duplicates from Sorted List

来源:互联网 发布:淘宝新店如何刷销量 编辑:程序博客网 时间:2024/04/29 18:41

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.

宝宝天天超时。。。简直你大爷。。。

/** * 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(NULL != head)          {              ListNode* ret = new ListNode(head->val);              ListNode* newList = ret;              ListNode* cur = head;              while(NULL != cur)              {                  if(cur->val != newList->val)                  {                      newList->next = new ListNode(cur->val);                      newList = newList->next;                  }                  cur = cur->next;              }              return ret;          }          return NULL;      }};

不递归就不超时了。。。

0 0
原创粉丝点击