leetcode 019 —— Remove Nth Node From End of List

原创 2015年07月09日 16:58:35

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.

思路:计算后移的次数即可

class Solution {
public:
	ListNode* removeNthFromEnd(ListNode* head, int n) {
		int move = heightOfList(head) - n;  //移动次数
		ListNode pre(0);
		ListNode *p=⪯
		p->next = head;
		ListNode* start = head;
		while (move){
			start = start->next;
			p = p->next;
			move--;
		}
		p->next = start->next;
		delete(start);
		return (&pre)->next;
	}
	int heightOfList(ListNode *head){
		int n = 0;
		//ListNode *a = head;
		while (head){
			head = head->next;
			n++;
		}
		return n;
	}
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)

[LeetCode] 019. Remove Nth Node From End of List (Easy) (C++/Python)
  • hcbbt
  • hcbbt
  • 2015-03-04 19:26
  • 2083

【LeetCode-面试算法经典-Java实现】【019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)】

【019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a l...

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 ex...

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 ex...

LeetCode 第十九题 Remove Nth Node From End of List

一,问题描述 1,删除一个单链表末尾开始算起的第n个结点,然后返回该单链表。 2,例如: 输入: 1->2->3->4->5 其中n=2; 输出: 1->2->3->5; 3,解题思...

Remove Nth node from End of list--LeetCode

题目: Given a linked list, remove the nth node from the end of list and return its head. For examp...

【LeetCode】Remove Nth Node From End of List

题目介绍: 下面给出两种算法: 算法一

[leetcode: Python]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 ...
  • NXHYD
  • NXHYD
  • 2017-05-19 08:28
  • 126

LeetCode#19. Remove Nth Node From End of List

题目:删除链表倒数第n个元素

LeetCode-19 Remove Nth Node From End of List(移除尾部第N个节点)

LeetCode-19 Remove Nth Node From End of List Given a linked list, remove the nth node from the end o...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)