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->1->1->2->3, return 2->3.

这个题目在函数里单独弄了ListNode作为新的head,这样的话在循环里面可以统一处理而不用把开头的特殊情况拿出来

//方法一:测试Accepted
class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
		bool signal = false;
		ListNode tempHead(0);
		if(!head) {
			return head;
		}
		ListNode *first = head, *second = head -> next, *pre = &tempHead;
		tempHead.next = head;
		while(second)
		{
			while(second && first -> val == second -> val)
			{
				signal = true;
				first = second;
				second = second -> next;
			}
			if(!signal)
			{
				pre = first;
				first = second;
				second = second -> next;
			}
			else
			{
				pre -> next = second;
				first = second;
				if(second)
					second = second -> next;
			}
			signal = false;
		}
		return tempHead.next;
    }
};
#include<iostream>

using namespace std;

#define N 7

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
		bool signal = false;
		ListNode tempHead(0);
		if(!head) {
			return head;
		}
		ListNode *first = head, *second = head -> next, *pre = &tempHead;
		tempHead.next = head;
		while(second)
		{
			while(second && first -> val == second -> val)
			{
				signal = true;
				first = second;
				second = second -> next;
			}
			if(!signal)
			{
				pre = first;
				first = second;
				second = second -> next;
			}
			else
			{
				pre -> next = second;
				first = second;
				if(second)
					second = second -> next;
			}
			signal = false;
		}
		return tempHead.next;
    }
};

ListNode *creatlist(){
	ListNode *list;
	list = NULL;
	ListNode *p;
	for(int i=0; i<N; i++)
	{
		int a;
		cin>>a;
		p = (ListNode*)malloc(sizeof(ListNode));
		p->val = a;
		p->next=list; //指定后继指针
		list = p; //head指针指定到新插入的结点上
	}
	return list;
}

int main()
{
	ListNode *list = creatlist();

	Solution lin;
	ListNode *outlist=lin.deleteDuplicates(list);
	for(int i=0; i<N; i++)
	{
		cout<<outlist->val;
		outlist = outlist->next;
	}
}
时间: 2024-10-09 06:26:01

leetcode_82_Remove Duplicates from Sorted List II的相关文章

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

LintCode - Remove Duplicates from Sorted List II Show result

LintCode - Remove Duplicates from Sorted List II Show result LintCode - Remove Duplicates from Sorted List II Show result Web Link Description Code - C Tips Web Link http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list-ii/ Descriptio

[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

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

80. Remove Duplicates from Sorted Array II

/* * 80. Remove Duplicates from Sorted Array II * 2016-5-13 by Mingyang * 这里用了通式,就是k的值可以根据需要随时改变,注意这里++j的用法,最后return ++j * 注意的是题目不光要返回int,还要把array给换了 */ public int removeDuplicates2(int[] A,int k){ int len=A.length; if(len<k) return len; int j=0; int

[LeetCode][JavaScript]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 List II

一.题目 Remove Duplicates from Sorted List II Total Accepted: 37833 Total Submissions: 151627My Submissions Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Give