runnig

[leetcode] 83 Remove Duplicates from Sorted List C++

Feb 3rd, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. /*
  2. http://leetcode.com/onlinejudge#question_83
  3.  
  4. Given a sorted linked list, delete all duplicates such that each element appear only once.
  5.  
  6. For example,
  7. Given 1->1->2, return 1->2.
  8. Given 1->1->2->3->3, return 1->2->3.
  9.  
  10. */
  11.  
  12. class Solution {
  13. public:
  14.     ListNode *deleteDuplicates(ListNode *head) {
  15.        
  16.         ListNode * a = head, *b;
  17.        
  18.         for(; a != NULL; a = a->next)
  19.         {
  20.             while(NULL != (b = a->next) && a->val == b->val)
  21.             {
  22.                 a->next = b->next;                
  23.             }
  24.         }
  25.         return head;
  26.        
  27.     }
  28. };
Advertisement
Add Comment
Please, Sign In to add comment