第16&17题 Remove Duplicates from Sorted List

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.

Solution:

<span style="font-weight: normal;">/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null || head.next==null) return head;
        ListNode prev = head, cur = head.next;
        while(cur!=null){
            if(prev.val==cur.val){//remove cur from the list
                prev.next=cur.next;
                cur=cur.next;
            }
            else{
                prev=prev.next;
                cur=cur.next;
            }
        }
        return head;

    }
}</span>

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->3.

<span style="font-size:18px;">/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null || head.next==null) return head;
        ListNode newHead = new ListNode(0);//create a new empty head, the value of the new head will not be counted in
        newHead.next=head;
        ListNode prev = newHead, cur=head;
        while(cur!=null&&cur.next!=null){
            if(cur.val!=cur.next.val){
                prev=prev.next;
                cur=cur.next;
            }else{
                while(cur.next!=null&&cur.next.val==cur.val)    cur=cur.next;
                //remove nodes from prev.next to cur
                prev.next=cur.next;
                cur=cur.next;
            }
        }
        return newHead.next;
    }
}</span>

注意while的判断条件必须是cur!=null并且cur.next!=null。当遍历到list中只剩一个元素时(cur.next==null),显然不存在可能相等的情况。

时间: 2024-07-31 05:27:33

第16&17题 Remove Duplicates from Sorted List的相关文章

leetcode_80题——Remove Duplicates from Sorted Array II(两个指针)

Remove Duplicates from Sorted Array II Total Accepted: 38480 Total Submissions: 125235My Submissions Question Solution Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,

leetcode_26题——Remove Duplicates from Sorted Array (string)

Remove Duplicates from Sorted Array Total Accepted: 57887 Total Submissions: 183534My Submissions Question Solution Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocat

第14&amp;15题 Remove Duplicates from Sorted Array I&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第26题--Remove Duplicates from Sorted Array

problem: 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 ar

LeetCode算法题-Remove Duplicates from Sorted List

这是悦乐书的第160次更新,第162篇原创 01 前情回顾 昨晚的爬楼梯算法题,有位朋友提了个思路,使用动态规划算法.介于篇幅问题,这里不细说动态规划算法,以后会在数据机构和算法的理论知识里细说. 昨晚的三个解法中,根据测试数据和结果,第三种解法是最优的,但是还能不能更进一步呢?经过推导,我们得知当n大于等于3的时候,f(n) = f(n-1)+f(n-2),也就是说我们只需要得到n的前面两位的结果即可,对此我们使用了数组,将每个值都存起来了,最后取出数组中的最后一位元素.那么是否可以将数组也省

[LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)

①中文题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1: 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2. 你不需要考虑数组中超出新长度后面的元素.示例 2: 给定 nums = [0,0,1,1,1,2,2,3,3,4], 函数应该返回新的长度 5, 并且原数组

leetcode第83题-Remove Duplicates from Sorted List

这道题与实现数组中的删除重复元素类似.我们来看一下具体的过程,首先要判断是否为空(在这个上面吃了大苦头),删除下一个相同的元素的时候要定义一个tmp元素,再free掉即可. #include<stdio.h> #include<stdlib.h> struct ListNode { int val; ListNode *next; }; ListNode *deleteDuplicates(ListNode *head) { if (head) { struct ListNode

【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 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. 题解:水题不解释 1 /** 2 * Definition for singly-linked list. 3 * str