Advertisement
yragi_san

Remove Duplicates from Sorted List

Mar 18th, 2023
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. /**
  2.  * Definition for singly-linked list.
  3.  * struct ListNode {
  4.  *     int val;
  5.  *     ListNode *next;
  6.  *     ListNode() : val(0), next(nullptr) {}
  7.  *     ListNode(int x) : val(x), next(nullptr) {}
  8.  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9.  * };
  10.  */
  11. class Solution {
  12. public:
  13.     ListNode* deleteDuplicates(ListNode* head) {
  14.         ListNode *current = NULL;
  15.         ListNode *anterior = NULL;
  16.         current = head;
  17.         while(current){
  18.             if(anterior != NULL && current->val == anterior->val){
  19.                 anterior->next = current->next;
  20.             }else anterior = current;
  21.             current = current->next;
  22.         }
  23.         return head;
  24.     }
  25. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement