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

Remove Duplicates from Sorted List I&&II

时间:2014-10-08 01:26:44      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   ar   for   strong   

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  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *deleteDuplicates(ListNode *head) {
12         if( !head ) return 0;
13         ListNode* pre = head;   //前驱
14         ListNode* cur = pre->next;  //当前
15         while( cur ) {
16             if( cur->val == pre->val ) {    //若与前驱相同,直接删掉
17                 ListNode* p = cur;
18                 cur = cur->next;
19                 pre->next = cur;
20                 delete p;
21             } else {    //不同直接下移
22                 cur = cur->next;
23                 pre = pre->next;
24             }
25         }
26         return head;
27     }
28 };

Remove Duplicates from Sorted List II

 Total Accepted: 21273 Total Submissions: 85697My Submissions

 

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

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

凡是重复的数据都要删除,可以设个标记,如果发现有重复节点,那么将其值赋予标记上,然后凡是遇到标记值的节点直接删除
 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *deleteDuplicates(ListNode *head) {
12         if( !head ) return 0;
13         const int INF = 0x3fffffff; //设置个不可能值
14         ListNode node(-1);  //布置头节点,便于删除
15         node.next = head;
16         ListNode* pre = &node;
17         ListNode* cur = head;
18         int needDelete = INF;   //便于逻辑判断
19         while( cur ) {
20             if( cur->val == needDelete ) {  //若等于需被删除值,那么直接删除
21                 ListNode* p = cur;
22                 cur = cur->next;
23                 pre->next = cur;
24                 delete p;
25             } else if( cur->next && cur->val == cur->next->val ) {  //若发现有重复元素,那么记录需要删除的值
26                 needDelete = cur->val;
27             } else {    //后移节点
28                 cur = cur->next;
29                 pre = pre->next;
30             }
31         }
32         return node.next;
33     }
34 };

这里的不可能值,如果可能的话,那么就需要加逻辑判断,否则就会出大bug,尤其是面试官询问的话

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *deleteDuplicates(ListNode *head) {
12         if( !head ) return 0;
13         ListNode node(-1);  //布置头节点,便于删除
14         node.next = head;
15         ListNode* pre = &node;
16         ListNode* cur = head;
17         while( cur && cur->next && cur->val != cur->next->val ) {
18             pre = cur;
19             cur = cur->next;
20         }
21         if( !cur->next ) return node.next;
22         int needDelete = cur->val;
23         while( cur ) {
24             if( cur->val == needDelete ) {  //若等于需被删除值,那么直接删除
25                 ListNode* p = cur;
26                 cur = cur->next;
27                 pre->next = cur;
28                 delete p;
29             } else if( cur->next && cur->val == cur->next->val ) {  //若发现有重复元素,那么记录需要删除的值
30                 needDelete = cur->val;
31             } else {    //后移节点
32                 cur = cur->next;
33                 pre = pre->next;
34             }
35         }
36         return node.next;
37     }
38 };

 

Remove Duplicates from Sorted List I&&II

标签:des   style   blog   http   color   io   ar   for   strong   

原文地址:http://www.cnblogs.com/bugfly/p/4010301.html

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