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.

题目大意:

去除有序链表内部相同元素,即相同元素只保留一个。


代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
  * 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  NULL;
         ListNode* p = head->next;
         ListNode* pre = head;
         int  cur = head->val;
         while (p != NULL)
         {
             
             if (cur == p->val)
             {
                 pre->next = p->next;
             }
             else
             {
                 cur = p->val;
                 pre = p;
             }
             p = p->next;
         }
         return  head;
     }
};

其他简洁做法:

1.双while

参考自:https://discuss.leetcode.com/topic/2168/concise-solution-and-memory-freeing


1
2
3
4
5
6
7
8
9
10
11
12
class  Solution {
public :
     ListNode *deleteDuplicates(ListNode *head) {
         ListNode* cur = head;
         while  (cur) {
             while  (cur->next && cur->val == cur->next->val)
                 cur->next = cur->next->next;
             cur = cur->next;
         }
         return  head;
     }
};

2.双指针

参考自:https://discuss.leetcode.com/topic/2168/concise-solution-and-memory-freeing

1
2
3
4
5
6
7
8
9
10
11
12
ListNode *deleteDuplicates(ListNode *head) {
     ListNode*cur=head,*tail=head;
     while (cur){
         if (cur->val!=tail->val){
             tail->next=cur;
             tail=cur;
         }
         cur=cur->next;
         tail->next=NULL;
     }
     return  head;
}




本文转自313119992 51CTO博客,原文链接:http://blog.51cto.com/qiaopeng688/1837260
相关文章
|
5月前
【力扣】-- 移除链表元素
【力扣】-- 移除链表元素
56 1
|
5月前
Leetcode第21题(合并两个有序链表)
这篇文章介绍了如何使用非递归和递归方法解决LeetCode第21题,即合并两个有序链表的问题。
78 0
Leetcode第21题(合并两个有序链表)
|
5月前
|
存储 分布式计算 NoSQL
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
52 3
|
5月前
LeetCode第二十四题(两两交换链表中的节点)
这篇文章介绍了LeetCode第24题的解法,即如何通过使用三个指针(preNode, curNode, curNextNode)来两两交换链表中的节点,并提供了详细的代码实现。
52 0
LeetCode第二十四题(两两交换链表中的节点)
|
5月前
Leetcode第十九题(删除链表的倒数第N个节点)
LeetCode第19题要求删除链表的倒数第N个节点,可以通过快慢指针法在一次遍历中实现。
61 0
Leetcode第十九题(删除链表的倒数第N个节点)
|
7月前
|
算法
LeetCode第24题两两交换链表中的节点
这篇文章介绍了LeetCode第24题"两两交换链表中的节点"的解题方法,通过使用虚拟节点和前驱节点技巧,实现了链表中相邻节点的交换。
LeetCode第24题两两交换链表中的节点
|
7月前
|
存储 算法
LeetCode第86题分隔链表
文章介绍了LeetCode第86题"分隔链表"的解法,通过创建两个新链表分别存储小于和大于等于给定值x的节点,然后合并这两个链表来解决问题,提供了一种简单易懂且操作原链表的解决方案。
LeetCode第86题分隔链表
|
7月前
|
存储 算法
LeetCode第83题删除排序链表中的重复元素
文章介绍了LeetCode第83题"删除排序链表中的重复元素"的解法,使用双指针技术在原链表上原地删除重复元素,提供了一种时间和空间效率都较高的解决方案。
LeetCode第83题删除排序链表中的重复元素
|
7月前
|
算法
LeetCode第23题合并 K 个升序链表
这篇文章介绍了LeetCode第23题"合并K个升序链表"的解题方法,使用分而治之的思想,通过递归合并链表的方式解决了这个难题。
LeetCode第23题合并 K 个升序链表
|
5月前
|
索引
力扣(LeetCode)数据结构练习题(3)------链表
力扣(LeetCode)数据结构练习题(3)------链表
132 0