资讯专栏INFORMATION COLUMN

[LintCode] Remove Duplicates form Sorted List I &a

int64 / 2493人阅读

摘要:和上一道题不同的地方就是,需要用双指针操作,且最后要返回,以防止结点即的情况返回错误的结果。令,用进行查重操作,是的前结点。当和等值的时候,后移至第一个不等值的点,用指向新的即可。

Remove Duplicates form Sorted List I Problem

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example

Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

Note

遇到后一个结点和当前结点相等,就用当前结点指向下一个的下一个结点。

Solution
public class Solution {
    public static ListNode deleteDuplicates(ListNode head) { 
        if (head == null) return head;
        ListNode node = head;
        while (node.next != null) {
            if (node.val == node.next.val) node.next = node.next.next;
            else node = node.next;
        }
        return head;
    }  
}
Remove Duplicates form Sorted List II Problem

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

Example

Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

Note

和上一道题不同的地方就是,需要用双指针操作,且最后要返回dummy.next,以防止head结点即duplicate的情况返回错误的结果。
pre = dummy, cur = head,用cur进行查重操作,precur的前结点。当curcur.next等值的时候,cur后移至第一个不等值的点,用pre指向新的cur即可。

Solution
public class Solution {
    public static ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy, cur = head;
        while (cur != null && cur.next != null) {
            if (cur.val == cur.next.val) {
                int val = cur.val;
                while (cur != null && cur.val == val) cur = cur.next;
                pre.next = cur;
            }
            else {
                pre = pre.next;
                cur = cur.next;
            }
        }
        return dummy.next;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65790.html

相关文章

  • [LintCode/LeetCode] Remove Duplicates from Sorted

    摘要:思路原数组长度为,则返回原数组长度不为,则至少有个元素。将所有不重复的数值赋给,而当和相等时,不做处理。最后返回的就是不同元素的个数,也是新数组的长度。只有在时,才对赋值。注意,每次初始化的时候要分两种情况,这就意味着从的时候开始遍历。 Remove Duplicates from Sorted Array I Problem Given a sorted array, remove ...

    WalkerXu 评论0 收藏0
  • leetcode82. Remove Duplicates from Sorted List II

    摘要:题目要求翻译将链表中重复的元素全部删除,返回新的头结点。相比于,这里将重复的元素全部删除。除此以外,我们还需要知道重复元素的前一个值和重复元素的最后一个值。如果存在重复值,则跳过重复值后,前节点不变,否则前节点跟随后节点同时向后移动。 题目要求 Given a sorted linked list, delete all nodes that have duplicate number...

    崔晓明 评论0 收藏0
  • 82. Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicatenumbers, leaving only distinct numbers from the original list. https://leetcode.com/problems... # Definition for singly-linked list. # c...

    zhou_you 评论0 收藏0
  • leetcode83 Remove Duplicates from Sorted List从有序链表

    摘要:题目要求从有序链表中删除重复的数字,并且返回删除后的头结点例如输入链表为返回这题和相似,只是数据结构从数组变成了链表若还有更好的思路,请多多指教想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众 题目要求: 从有序链表中删除重复的数字,并且返回删除后的头结点例如输入链表为1->1->2,返回1->2 这题和leetcode26相似,只是数据结构从数组变成了链表 /*...

    JessYanCoding 评论0 收藏0
  • [Leetcode] Remove Duplicates from Sorted Array 移除有

    摘要:双指针法复杂度时间空间思路我们可以将不重复的序列存到数列前面,因为不重复序列的长度一定小于等于总序列,所以不用担心覆盖的问题。代码双指针法复杂度时间空间思路思路和上题一样,区别在于记录前两个遍历到的数字来帮助我们判断是否出现了第三遍。 Remove Duplicates from Sorted Array I Given a sorted array, remove the dupl...

    kel 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<