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, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

Hide Tags

Linked List Two Pointers

Have you met this question in a real interview?

Yes

No

Discuss

这道题题目要求删除链表中指定的倒数第n个结点,并且采用一次遍历,所以应该采用两个指针的方法

先将指针ptr1和ptr2之间的距离弄成n,再往后同时后移,而由于涉及到结点的删除,还要弄一个指针指到ptr1的前面那个结点处就可以了,

这道题要考虑到结点删除时是尾结点还是头结点还是中间结点

#include<iostream>
using namespace std;
struct ListNode {
	     int val;
	     ListNode *next;
		 ListNode(int x) : val(x), next(NULL) {}
	};

ListNode* removeNthFromEnd(ListNode* head, int n) {
	ListNode *ptr1,*ptr2,*ptr0;
	ptr0=head;
	ptr1=head;
	ptr2=head;
	int N=n-1;
	while(N--)
		ptr2=ptr2->next;

	if(ptr2->next==NULL)
		if(ptr1->next!=NULL)
			return ptr1->next;
		else
			return NULL;
	ptr1=ptr1->next;
	ptr2=ptr2->next;
	while(ptr2->next!=NULL)
	{ptr2=ptr2->next;ptr1=ptr1->next;ptr0=ptr0->next;}
	if(n==0)
	{ptr1->next=NULL;return head;}
    ptr0->next=ptr1->next;
	return head;
}
int main()
{

}

  

时间: 2024-11-08 05:31: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 链表

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 lis

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 *removeNthFromE

【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

【LeetCode】Remove Nth Node From End of List (2 solutions)

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

[leetcode]Remove Nth Node From End of List @ Python

原题地址:http://oj.leetcode.com/problems/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 n

Remove Nth Node From End of List leetcode java

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

[Lintcode]174. Remove Nth Node From End of List/[Leetcode]

174. Remove Nth Node From End of List/19. Remove Nth Node From End of List 本题难度: Easy/Medium Topic: Linked List Description Given a linked list, remove the nth node from the end of list and return its head. Example Example 1: Input: list = 1->2->3-&

Merge Two Sorted Lists &amp; Remove Nth Node From End of List

1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 2.删除list倒数第n个元素 Remove Nth Node From End of List Given a linked list,