leetcode第203题-Remove Linked List Elements

题目要求:从一个链表中删除指定的元素。

分析:首先要考虑链表的非空问题,我想这应该是判断链表和数组问题最首先要考虑处理的问题。其次是如果前几个元素都是与指定元素相同,也要做一次处理,使head指向(在非空的情况下)与指定元素不相等的第一个元素。注意:在这里我碰到了一个很可笑的问题,就是while里面循环的判断条件,应该让head!=NULL在前面,这样就能保证head->val的值的问题,一直在这里出问题,后来才检查出来。然后再定义两个指针,第一个指针p指向当前的元素,第二个指针指向下一个节点,剩下的判断就应该很简单了。

#include<stdio.h>
#include<stdlib.h>

struct ListNode
{
	int val;
	struct ListNode *next;
};

ListNode* removeElements(ListNode *head,int val)
{
	if (head==NULL)
		return NULL;
	struct ListNode *p, *q;
	while ((head != NULL)&&(head->val) == val)
	{
		p=head->next;
		free(head);
		head=p;
	}
	if (head==NULL)
		return NULL;
	p=head;
	q=head->next;
	while ((q!=NULL))
	{
		if (q->val != val)
		{
			p=q;
			q=q->next;
		}
		else
		{
			p->next = q->next;
			free(q);
			q=p->next;
		}
	}
	return head;
}

int main()
{
	int m;
	while(scanf("%d",&m)!=EOF)
	{
		ListNode *head=(ListNode *)malloc(sizeof(ListNode));
		ListNode *p=head;
		head->next=NULL;
		for(int i=0;i<m;i++)
		{
			int tmp;
			scanf("%d",&tmp);
			ListNode *q=(ListNode *)malloc(sizeof(ListNode));
			q->val=tmp;
			p->next=q;
			p=q;
			p->next=NULL;
		}
		int n;
		scanf("%d",&n);
		ListNode *h=removeElements(head->next,n);
		while(h)
		{
			printf("%d ",h->val);
			h=h->next;
		}
		printf("\n");
	}
	return 0;
}
时间: 2024-08-09 16:17:07

leetcode第203题-Remove Linked List Elements的相关文章

[LC]203题 Remove Linked List Elements (移除链表元素)(链表)

①英文题目 Remove all elements from a linked list of integers that have value val. Example: Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5 ②中文题目 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val =

LeetCode【203】Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 很简单吧,没什么好说的.直接上AC代码: ListNode* removeElements(ListNode* hea

leetcode_203题——Remove Linked List Elements(链表)

Remove Linked List Elements Total Accepted: 8053 Total Submissions: 29898My Submissions Question Solution Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val =

(LeetCode 203)Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,  val = 6Return: 1 --> 2 --> 3 --> 4 –> 5 题目要求: 删除链表中包含val的元素结点 解题思路: 重点在于找到第一个非val的头结点,然后遍历链表,依次删除值为

LeetCode算法题-Remove Linked List Elements(Java实现)

这是悦乐书的第189次更新,第191篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第48题(顺位题号是203).移除单链表中节点值为val的节点.例如: 输入:1-> 2-> 6-> 3-> 4-> 5-> 6,val = 6 输出:1-> 2-> 3-> 4-> 5 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 02 第一种解法 特殊情况

leetCode 203. Remove Linked List Elements 链表

203. Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 题目大意: 删除链表中全部的目标元素. 代码如下:

203. Remove Linked List Elements - LeetCode

Question 203.?Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 Java实现: public ListNode removeElements(ListNode head, int val) { ListNode cur = head; while (cur != null) { if (cur.next != null && cur.next.val ==

leetcode Remove Linked List Elements 203

Remove Linked List Elements Total Accepted: 11291 Total Submissions: 42824 Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3

[LeetCode][JavaScript]Remove Linked List Elements

Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 https://leetcode.com/problems/r