leetcode (82, 83) remove duplicates from sorted list i, ii

Remove Duplicates from Sorted List

Leetcode 83. Remove Duplicates from Sorted List

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.

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* prev = head;
        while (prev) {
            if (prev->next && prev->val == prev->next->val)
                prev->next = prev->next->next;
            else
                prev = prev->next;
        }
        
        return head;
    }
};

Leetcode 82. Remove Duplicates from Sorted List

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,

Given 1->2->3->3->4->4->5, return 1->2->5.

Given 1->1->1->2->3, return 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) {
        ListNode* prevhead = new ListNode(0);
        ListNode* prev = head;
        ListNode* point = prevhead;
        
        while (prev) {
            if (prev->next && prev->val == prev->next->val)
                while (prev->next && prev->val == prev->next->val)
                    prev = prev->next;
            else {
                point->next = prev;
                point = point ->next;
            }
            prev = prev->next;
        }
        point->next = NULL;
        return prevhead->next;
    }
};