leetcode第19题-Remove Nth Node From End of List

本题比较简单,主要考察了单链表的创建与删除。

但是有一个问题需要着重的考虑,如何快速定位链表的倒数第n个节点。这就需要两个辅助节点,一个节点先走到正数第n个位置,然后两个辅助节点一块往后走,最后后面的节点的位置就是我们需要的倒数第n个节点。

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

struct ListNode//定义节点
{
	int value;
	struct ListNode *next;
};

ListNode *removeNthFromEnd(ListNode *head,int n)
{
	ListNode *p=head;//前面开始走的节点
	ListNode *q=head;//后面跟着走的节点
	while(p!=NULL)
	{
		p=p->next;
		if(n--<0)
			q=q->next;//让q节点走到可以删除的位置
	}
	if(n==0)//表示就从开头开始删除
		head=head->next;
	else//正常的位置删除节点
		q->next=q->next->next;
	return head;
}

int main()
{
	int n,m;
	while(scanf("%d",&m)!=EOF)
	{
		ListNode *head=(ListNode*)malloc(sizeof(ListNode));
		head->next=NULL;
		head->value=0;
		ListNode *p=head;
		for(int i=0;i<m;i++)
		{
			int tmp_value;
			ListNode *tmp_node=(ListNode*)malloc(sizeof(ListNode));
			scanf("%d",&tmp_value);
			tmp_node->value=tmp_value;
			tmp_node->next=NULL;
			p->next=tmp_node;
			p=tmp_node;
		}
		scanf("%d",&n);
		ListNode*h=removeNthFromEnd(head,n);
		while(h->next)
		{
			h=h->next;
			printf("%d ",h->value);
		}
	}
	return 0;
}
时间: 2024-08-06 03:46:32

leetcode第19题-Remove Nth Node From End of List的相关文章

LeetCode 第 19 题 (Remove Nth Node From End of List)

LeetCode 第 19 题 (Remove Nth Node From End of List) Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the li

LeetCode(19) Remove Nth Node From End of List

题目 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Giv

leetcode_19题——Remove Nth Node From End of List(链表)

Remove Nth Node From End of List Total Accepted: 54129 Total Submissions: 197759My Submissions Question Solution Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5,

LeetCode 19: Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given

LeetCode之“链表”:Remove Nth Node From End of List

题目链接 题目要求: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. N

LeetCode(19) - Remove Nth Node From End of List

题目要求是,给你一个单向链表,和一个数字n,删除该链表倒数第n个node,其中测试案例中n能保证一定有效. 思路很简单,用两个指针,它们两个相隔为n,当后面的指针指向链表尾的时候,前面的指针指向的node的下一个node,就是要删除的那一个. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int

19:Remove Nth Node From End of List【两指针】【链表】

题目连接:https://leetcode.com/problems/remove-nth-node-from-end-of-list/ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /*题意:删除链表中从最后一个结点向前的第n个结点 */ /** *思路:1.两个

[LeetCode][JavaScript]Remove Nth Node From End of List

https://leetcode.com/problems/remove-nth-node-from-end-of-list/ Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After

【leetcode刷题笔记】Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n