Remove Duplicates from Sorted List leetcode

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.

题目意思为删除链表中重复的元素

思路: 遍历链表,用两个指针,一个指向前一个,一个指向后一个,当两个所指向的值不相等时,同时往后移,当两个指向的值相等时,需要删除一个元素(后一个指针指向的值),然后后一个往后遍历

代码如下:

<span style="font-size:18px;">/**
 * 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 *p,*q,*pur;

    p=head;

    q=p->next;
    while(q!=NULL)
    {
        if(p->val==q->val)
        {
            pur=q;
            q=q->next;
            p->next=q;
            delete pur;
        }
        else
        {
            p=q;
            q=q->next;
        }
    }

        return head;

    }
};</span>
时间: 2024-10-03 16:11:58

Remove Duplicates from Sorted List leetcode的相关文章

Remove Duplicates from Sorted List leetcode java

题目: 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 Array leetcode java

算法描述: 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

Remove Duplicates from Sorted Array -- leetcode

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 = 

83. Remove Duplicates from Sorted List Leetcode Python

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. 定义一个pre 和cur 1.当二者不等的时候pre.next=cur pre=pre.next 2.当相等的时候pr

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; 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 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所指的元素和

LeetCode:Remove Duplicates from Sorted Array &amp;&amp; Remove Element

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 =

[leetcode笔记] 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->1->1->2->3, return 2-&

LeetCode——Remove Duplicates from Sorted Array

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 =