remove-duplicates-from-sorted-list I&II——去除链表中重复项

I、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.

PS:遍历,而后记录pre,并删除后续重复node

 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         ListNode *cur=head,*pre=NULL;
13         while(cur!=NULL){
14             if(pre==NULL){
15                 pre=cur;
16                 cur=cur->next;
17                 continue;
18             }
19             ListNode *next=cur->next;
20             if(pre->val==cur->val){
21                 pre->next=next;
22             }else
23                 pre=cur;
24             cur=next;
25         }
26         return head;
27     }
28 };

II、

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

For example,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.

删除所有重复项。

PS:循环比较next->val==cur->val,若next跳动则剔除cur至next之间的节点。否则右移left指针。

 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==NULL) return NULL;
13         ListNode h(-1);
14         ListNode *res=&h;
15         res->next=head;
16         ListNode *cur=head,*pre=NULL,*left=res;
17         while(cur!=NULL){
18             ListNode* next=cur->next;
19             while(next!=NULL&&cur->val==next->val){
20                 next=next->next;
21             }
22             if(next==cur->next){
23                 left=cur;
24             }else{
25                 left->next=next;
26             }
27             cur=next;
28         }
29         return res->next;
30     }
31 };
时间: 2024-10-24 03:06:35

remove-duplicates-from-sorted-list I&II——去除链表中重复项的相关文章

leetCode 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. 思路:此题与上一题异曲同工,具体解法如下: /** * Definition for singly-linked li

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. 移除有序链表中的重复项需要定义个指针指向该链表的第一个元素,然后第一个元素和第二个元素比较,如果重复了,则删掉第二个元素,

【LeetCode每天一题】Remove Duplicates from Sorted List(移除有序链表中的重复数字)

Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2 Example 2: Input: 1->1->2->3->3 Output: 1->2->3 思路 对于链表类的题目主要考的是指针的操作,他需要对指针的指向节点有正确的操作.这道题我们可以使用从

力扣—Remove Duplicates from Sorted List(删除排序链表中的重复元素)python实现

题目描述: 中文: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2 示例 2: 输入: 1->1->2->3->3输出: 1->2->3 英文: Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->

[CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项

2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed? 这道题让我们移除无序链表中的重复项,在LeetCode中有两道类似的题是Remove Duplicates from Sorted List 移除有序链表中的重复项 和 Remove Duplicates fr

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. 如果下一个节点和当前节点的值一样,就继续往后.这

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. 这题思路很清楚,当前节点的值与前一节点的值相同,那么

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