[LeetCode]82. 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.

Subscribe to see which companies asked this question

解法:设置两个指针curr和next指向相邻两个节点,从头往后扫描,(1)如果某次指向的两个节点值相等,则删除next指向的节点,并且next前移;(2)如果指向的两个节点值不一样,则两个节点都向前移动。

/**
 * 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(head == NULL || head->next == NULL) return head;
        ListNode *curr = head, *next = head->next;
        while(next != NULL) {
            if(curr->val == next->val) {
                ListNode* del = next;
                next = next->next;
                curr->next = next;
                delete del;
            }
            else {
                curr = next;
                next = next->next;
            }
        }
        return head;
    }
};

或者用一个指针:

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

需要注意的一点是可能某个重复值出现了超过2次,所以在找到重复值时不能两个指针同时前移。

时间: 2024-12-03 15:47:10

[LeetCode]82. 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. Example 1: Input: 1->1->2 Output: 1->2 Example 2: Input: 1->1->2->3->3 Output: 1->2->3 题意: 有序链表去重 思路: 代码: 1 class Solution { 2 public Lis

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 82.Remove Duplicates from Sorted List II (删除排序链表的反复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->1->1->2->3, return 2->3

leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的重复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->1->1->2->3, return 2->3

Leetcode 82 Remove Duplicates from Sorted List 2

* Problem: * Given a sorted linked list, delete all nodes that have duplicate numbers * leaving only distinct numbers from the original list. * Solution: * Compare the current position with the previous and behind it. If it does not equal to them, th

[leetcode]82. Remove Duplicates from Sorted List

第一题:遍历链表,遇到重复节点就连接到下一个. public ListNode deleteDuplicates(ListNode head) { if (head==null||head.next==null) return head; ListNode res = head; while (head.next!=null){ if (head.val==head.next.val) { if (head.next.next!=null) head.next = head.next.next;

[LeetCode 题解]: 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. 题意:给定一个已排序的链表,删除其中重复的元素,保证每个元素有且仅有一个. 使用两个指针,分别指向前一个元素,和当前元素,

82. Remove Duplicates from Sorted List II && i

题目 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. 解析 [LeetCode] Rem

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {