LeetCode刷题笔记(链表):remove-duplicates-from-sorted-list



[toc]

题目描述

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

For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->2->3.

解题思路

简单问题不要想的太复杂,直接嵌套循环搞定喽。外循环用来遍历链表,内循环用来遍历重复元素,如果重复就一直传递指针。

C++版代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 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 *dummy = head;
while(dummy != NULL){
while(dummy->next != NULL && dummy->val == dummy->next->val){
dummy->next = dummy->next->next;
}
dummy = dummy->next;
}
return head;
}
};

系列教程持续发布中,欢迎订阅、关注、收藏、评论、点赞哦~~( ̄▽ ̄~)~

完的汪(∪。∪)。。。zzz

0%