Remove Duplicates from Sorted List I&&II

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 };
时间: 2024-10-29 13:08:31

Remove Duplicates from Sorted List I&&II的相关文章

Leetcode | Remove Duplicates from Sorted Array I && II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

Leetcode | Remove Duplicates from Sorted List I && II

Remove Duplicates from Sorted List I 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 Solutions : Remove Duplicates from Sorted List I & II

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. /** * Definition for sin

第14&15题 Remove Duplicates from Sorted Array I&II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

Remove Duplicates from Sorted Array I&&II

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A = [

怎样防超时——Remove Duplicates from Sorted Array I&&II

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array nums 

Remove Duplicates from Sorted List I & II

Title: 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. /** * Definition for singly-linked list. * struct List

LeetCode(82): Remove Duplicates from Sorted List II

Remove Duplicates from Sorted List II: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->

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和