Remove Duplicates from Sorted List II--LeetCode

题目:

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.

思路:先锁定头,然后处理中间位置,记得最后处理尾部,知识繁琐。处理头部时。找到一个节点,当前节点没有同样的连续节点,同一时候节点和后序节点不同。处理中间仅仅须要记录前序节点和遍历节点就可以。使得遍历节点没有连续同样的节点。假设有连续同样的节点,那么删除全部的连续同样的节点

#include <iostream>
#include <vector>
using namespace std;

typedef struct list_node List;
struct list_node
{
	int value;
	struct list_node* next;
};

void Init_List(List*& head,int* array,int n)
{
    head = NULL;
    List* tmp;
    List* record;
    for(int i=1;i<=n;i++)
    {
        tmp = new List;
        tmp->next = NULL;
        tmp->value = array[i-1];
        if(head == NULL)
        {
            head = tmp;
            record = head;
        }
        else
        {
            record->next = tmp;
            record = tmp;
        }
    }
}
void print_list(List* list)
{
    List* tmp=list;
    while(tmp != NULL)
    {
        cout<<tmp->value<<endl;
        tmp = tmp->next;
    }
}  

void RemoveDuplicate(List*& head)
{
	if(head == NULL || head->next==NULL)
		return ;
	 List* pre=head;
	 List* cur;
	 List* fast;
	 if(head->value == head->next->value)
	{
	 while(1)
	 {
	 	while(pre != NULL &&pre->next != NULL)
		 {
	 		if(pre->value == pre->next->value)
	 			pre = pre->next;
	 		else
	 			break;
	 	}
	 	pre = pre->next;
	 	if(pre->next == NULL || pre->value != pre->next->value)
	 	{
	 		head = pre;
	 		break;
	 	}
	 }
	}
	 if(head == NULL)
	 	return ;
	 pre = head;
	 cur=head->next;
	 while(cur !=NULL && cur->next != NULL)
	 {
	 	if(cur->value != cur->next->value)
	 	{
	 		if(pre->next != cur) // 不相邻
			{
				cur = cur->next;
				pre->next = cur;
			}
			else
			{
				pre = cur;
				cur = cur->next;
			}
	 	}
	 	else
	 	{
	 		cur = cur->next;
	 	}
	 }
	if(pre->next!=NULL && pre->next->next !=NULL)
	{
		if(pre->next->value == pre->next->next->value)
			pre->next = NULL;
	}
} 

int main()
{
	int array[]={1,1,1,2,2,3,4,4,4,5,5,7,7,8};
	List* head;
	Init_List(head,array,sizeof(array)/sizeof(int));
	RemoveDuplicate(head);
	print_list(head);
	return 0;
}

PS:看到每个题目,首先尽可能考虑全面,并且要考虑怎样使用更加简洁的思路来解决这个问题,在这个题目中,首先须要考虑刚開始就有不符合条件的节点,那么第一步须要处理的就是找到终于的头部节点,然后再解决删除节点,那么在删除的时候每次考虑两个节点。考虑这两个相邻的节点是否一样,假设一样。那么就是循环删除当前节点值的过程。假设不同,那么就须要将第一个节点插入到终于的链表中。

时间: 2024-10-06 00:30:50

Remove Duplicates from Sorted List II--LeetCode的相关文章

Remove Duplicates from Sorted List II leetcode java

题目: 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-&

Remove Duplicates from Sorted Array II leetcode java

题目: 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]. 题解: 之前是不允许有重复的. 现在是可以最多允许2个一样的元素. 然后删除dupli

Remove Duplicates from Sorted Array II ——LeetCode

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't

Remove Duplicates from Sorted Array II -- leetcode

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]. 加入一个计数器,用于统计重复字符的个数. class Solution { public: i

Remove Duplicates from Sorted List II ——LeetCode

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.

82. Remove Duplicates from Sorted List II Leetcode Python

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

(每日算法)LeetCode --- Remove Duplicates from Sorted Array II (删除重复元素II)

Remove Duplicates from Sorted Array II Leetcode 题目: 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

【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所指的元素和

[leetcode笔记] 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->1->1->2->3, return 2-&

[leetcode]Remove Duplicates from Sorted List II @ Python

原题地址:https://oj.leetcode.com/problems/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-&g