LeetCode 83 Remove Duplicates from Sorted List(从已排序链表中移除重复元素)(Linked List)(*)

简介: 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50514648 翻译给定一个已排序链表,删除所有重复元素让每个元素只出现一次。
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50514648

翻译

给定一个已排序链表,删除所有重复元素让每个元素只出现一次。

例如:
给定 1->1->2, 返回 1->2。
给定 1->1->2->3->3, 返回 1->2->3

原文

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 206 Reverse Linked List(反转链表)(四步将递归改写成迭代)(*) ,然后再回过来看,因为这一篇中我用作图的方式解释了针对反转链表的一系列过程,本题也是大同小异的。

切入正题,首先我们应该先判断是否为空:

if (head == NULL) return NULL;
if (head->next == NULL) return head;    

但是这两行代码是可以简写的:

if (head == NULL || head->next == NULL) return head;

下面是一个循环语句:

while (head->next) {
    if (head->val == head->next->val) {
        head->next = head->next->next;
    }
    else {
        head = head->next;
    }   
}    

意思非常简单,首先判断当前的节点的值与下一个节点的值是否相等,如果相等,则将下下一个节点赋值给下一个节点。

1——1——2——3——3
改写成:
1——2——3——3

此时head还在1上,但为什么不将其移动到2上呢,也就是说代码为什么不是这样的:

while (head->next) {
    if (head->val == head->next->val) {
        head->next = head->next->next;
        head = head->next;
    }
    else {
        head = head->next;
    }   
}    

理由很简单,例如:

1——1——1——2——3——3
经过第一次变换后:
1——1——2——3——3
如果贸然将head移动到下一个节点上,那么下一次的对比就是对比1和2了,显然是不合理的。

到了这里移除操作就已经完成了,但是能够直接returnhead吗,显然也是不能的,因为head已经移动到了最后一个节点了。

所以应该在while循环之前就设置了新的head作为记录,最后返回它就好了。

ListNode* newHead = head;
while(){}
return newHead;

代码

C Plus Plus

/**
* 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* newNode = head;
        while (head->next) {
            if (head->val == head->next->val)
                head->next = head->next->next;
            else
                head = head->next;
        }
        return newNode;
    }
};

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode c = head;
        while (c.next != null) {
            if (c.val == c.next.val) {
                c.next = c.next.next;
            } else {
                c = c.next;
            }
        }
        return c;
    }
}
目录
相关文章
|
1月前
《剑指offer》——合并两个排序的链表
《剑指offer》——合并两个排序的链表
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
|
18天前
List中的remove方法遇到报错不能删除以及四种解决办法点赞收藏
List中的remove方法遇到报错不能删除以及四种解决办法点赞收藏
16 0
|
22天前
使用List中的remove方法遇到数组越界
使用List中的remove方法遇到数组越界
13 2
|
1月前
|
存储 JavaScript
leetcode82. 删除排序链表中的重复元素 II
leetcode82. 删除排序链表中的重复元素 II
22 0
|
1月前
leetcode83. 删除排序链表中的重复元素
leetcode83. 删除排序链表中的重复元素
10 0
|
1月前
|
算法
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
|
1月前
|
存储
LeetCode刷题---817. 链表组件(哈希表)
LeetCode刷题---817. 链表组件(哈希表)
|
1月前
|
存储 C语言 索引
环形链表、环形链表 II、有效的括号​​​​​​​【LeetCode刷题日志】
环形链表、环形链表 II、有效的括号​​​​​​​【LeetCode刷题日志】
|
1月前
|
算法 安全 数据处理
LeetCode刷题---707. 设计链表(双向链表-带头尾双结点)
LeetCode刷题---707. 设计链表(双向链表-带头尾双结点)