LeetCode(83)题解: Remove Duplicates from Sorted List

https://leetcode.com/problems/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.

思路:

考察指针和链表的基本使用。

AC代码:

/**
 * 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)
        return head;
    int pre=head->val;
    ListNode * p=head;
    while(p->next!=NULL){
        if(p->next->val==pre){
            p->next=p->next->next;
        }
        else{
        p=p->next;
        pre=p->val;
        }
    }
    return head;
    }
};
时间: 2024-10-05 07:16:41

LeetCode(83)题解: Remove Duplicates from Sorted List的相关文章

LeetCode(26)题解:Remove Duplicates from Sorted Array

https://leetcode.com/problems/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

(LeetCode 83)Remove Duplicates from Sorted Lists

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. 题目要求: 给一有序链表,删除重复的结点,使得每个元素只出现一次. 解题思路: 1.从头到尾遍历链表,如果前后两个结点相同

leetcode || 83、Remove Duplicates from Sorted List

problem: 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. Hide Tags Linked List 题意:将一个已经排好序的单链表 中的重复元素删除,只留一

LeetCode(82)题解: Remove Duplicates from Sorted List II

https://leetcode.com/problems/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->

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

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

Leetcode 线性表 Remove Duplicates from Sorted Array

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array Total Accepted: 14789 Total Submissions: 46601 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new l

Leetcode 线性表 Remove Duplicates from Sorted List

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List Total Accepted: 14961 Total Submissions: 43772 Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1

Leetcode 线性表 Remove Duplicates from Sorted List II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted List II Total Accepted: 10702 Total Submissions: 43714 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from th