码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 面试题 02.01. 移除重复节点

时间:2020-02-27 17:34:36      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:dup   链表   null   color   head   面试   moved   编写   remove   

编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。

示例1:

输入:[1, 2, 3, 3, 2, 1]
输出:[1, 2, 3]
示例2:

输入:[1, 1, 1, 1, 2]
输出:[1, 2]
提示:

链表长度在[0, 20000]范围内。
链表元素在[0, 20000]范围内。

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 
 9 struct ListNode* removeDuplicateNodes(struct ListNode* head){
10     if(head==NULL||head->next==NULL) return head;
11     int index[20005]={0};
12     index[head->val]=1;
13     struct ListNode *pre=head,*q=head->next;
14     while(q){
15         if(index[q->val]==0){
16             index[q->val]=1;
17             pre=q;
18             q=q->next;
19         }else{
20             pre->next=q->next;
21             q=pre->next;
22         }
23     }
24     return head;
25 }

 

LeetCode 面试题 02.01. 移除重复节点

标签:dup   链表   null   color   head   面试   moved   编写   remove   

原文地址:https://www.cnblogs.com/shixinzei/p/12373187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!