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.

思路:两个指针,一个指向当前节点cur,一个指向下一个节点nx,终止条件,也就是比较了len-1次。

比较若不等,则两指针均后移,若相等则删除节点,且cur指针不变,nx指针后移。

代码:

class Solution {
public:
    int getLength(ListNode *head){
        int length=0;
        while(head){
            ++length;
            head=head->next;
        }
        return length;
    }
    ListNode *deleteDuplicates(ListNode *head) {
        if(head==NULL) return NULL;
        int len=getLength(head);
        int i=1;
        ListNode* res=head;
        ListNode* cur=res;
        ListNode* nx=res->next;
        while(i<len){
            if(cur->val==nx->val){
                cur->next=cur->next->next;
                nx=nx->next;
            }else{
                cur=cur->next;
                nx=nx->next;
            }
            ++i;
        }
        return res;
    }
};
时间: 2024-10-07 20:22:55

Remove Duplicates from Sorted List (链表)的相关文章

leetCode 83. Remove Duplicates from Sorted List 链表

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. 题目大意: 去除有序链表内部相同元素,即相同

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 singly-linked list. * public class ListN

[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. Hide Tags Linked List class Solution { public: ListNode *dele

Remove Duplicates from Sorted List ,除去链表中相邻的重复元素

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. public ListNode deleteDu

LeetCode之“链表”:Remove Duplicates from Sorted List &amp;&amp; Remove Duplicates from Sorted List II

1. 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(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 List II @ Python

原题地址:https://oj.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-&g

[LeetCode][JavaScript]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][JavaScript]Remove Duplicates from Sorted List

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

LeetCode: Remove Duplicates from Sorted List II [083]

[题目] 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-