leetcode第83题-Remove Duplicates from Sorted List

这道题与实现数组中的删除重复元素类似。我们来看一下具体的过程,首先要判断是否为空(在这个上面吃了大苦头),删除下一个相同的元素的时候要定义一个tmp元素,再free掉即可。

#include<stdio.h>
#include<stdlib.h>
struct ListNode
{
	int val;
	ListNode *next;
};

ListNode *deleteDuplicates(ListNode *head)
{
	if (head)
	{
        struct ListNode *p = head;
		while (p->next)
		{
			if (p->val != p->next->val)
			{
				p = p->next;
			}
			else
			{
				struct ListNode *tmp = p->next;
				p->next = p->next->next;
				free(tmp);
			}
		}
	}
	return head;
}

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		ListNode *head=(ListNode *)malloc(sizeof(ListNode));
		ListNode *p=head;
		p->next=NULL;
		for(int i=0;i<n;i++)
		{
			int tmp;
			scanf("%d",&tmp);
			ListNode *q=(ListNode *)malloc(sizeof(ListNode));
			q->val=tmp;
			p->next=q;
			p=q;
			p->next=NULL;
		}
		ListNode *afterHead=deleteDuplicates(head->next);
		while(afterHead)
		{
			printf("%d ",afterHead->val);
			afterHead=afterHead->next;
		}
		printf("\n");

	}
	return 0;
}
时间: 2024-08-30 07:17:25

leetcode第83题-Remove Duplicates from Sorted List的相关文章

leetcode第26题--Remove Duplicates from Sorted Array

problem: 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 ar

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. 简单的题目,直接上AC代码: ListNode* deleteDuplicates(ListNode* head) { i

leetcode_80题——Remove Duplicates from Sorted Array II(两个指针)

Remove Duplicates from Sorted Array II Total Accepted: 38480 Total Submissions: 125235My Submissions Question Solution Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,

LeetCode记录之26——Remove Duplicates from Sorted Array

国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等.拿起键盘干就行了.然后返回有序项的下标就可以. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not

LeetCode之“链表”:Remove Duplicates from Sorted List &amp;&amp; Remove Duplicates from Sorted List II

1. 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_26题——Remove Duplicates from Sorted Array (string)

Remove Duplicates from Sorted Array Total Accepted: 57887 Total Submissions: 183534My Submissions Question Solution Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocat

第14&amp;15题 Remove Duplicates from Sorted Array I&amp;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

第16&amp;17题 Remove Duplicates from Sorted List

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. Solution: <span sty

LeetCode(2) Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array 题目 考察数组 描述 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 const