Remove Duplicates from Sorted List I & II

Title:

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) {
        ListNode* p = head;
        ListNode* pre = NULL;
        while (p != NULL){
            if (pre != NULL && p->val == pre->val){
                    pre->next = p->next;
                    delete p;
                    p = pre->next;
            }
            else{
                    pre = p;
                    p = p->next;
            }
        }
        return head;
    }
};

Title:

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.

class Solution{
    public:
ListNode *deleteDuplicates(ListNode *head) {
        if(head == NULL || head->next == NULL){
            return head;
        }
        ListNode *p = new ListNode(-1);
        p->next = head;
        ListNode *cur = p, *pre = head;
        while(pre != NULL){
            bool isDupli = false;
            while(pre->next != NULL && pre->val == pre->next->val){
                isDupli = true;
                pre = pre->next;
            }
            if(isDupli){
                pre = pre->next;
                continue;

            }
            cur->next = pre;
            cur = cur->next;
            pre = pre->next;

        }
        cur->next = pre;
        return p->next;
    }
};
时间: 2024-10-11 16:37:57

Remove Duplicates from Sorted List I & II的相关文章

Leetcode | Remove Duplicates from Sorted Array I && II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

Leetcode | Remove Duplicates from Sorted List I && II

Remove Duplicates from Sorted List I 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. 如果下一个节点和当前节点的值一样,就继续往后.这

Remove Duplicates from Sorted List I&&II

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. 这题思路很清楚,当前节点的值与前一节点的值相同,那么

LeetCode Solutions : Remove Duplicates from Sorted List I & II

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. /** * Definition for sin

第14&15题 Remove Duplicates from Sorted Array I&II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

Remove Duplicates from Sorted Array I&&II

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A = [

怎样防超时——Remove Duplicates from Sorted Array I&&II

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array nums 

LeetCode(82): Remove Duplicates from Sorted List II

Remove Duplicates from Sorted List II: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->

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和