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-面试算法经典-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] 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
  • 2170

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...
  • LZTree
  • LZTree
  • 2017年03月26日 21:28
  • 94

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...
  • buptlrw
  • buptlrw
  • 2016年02月19日 18:47
  • 330

《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, ...

[C++]LeetCode 19: Remove Nth Node From End of List(删除链表中倒数第n个节点)

Problem: Given a linked list, remove the nth node from the end of list and return its head. For exam...

Leetcode_19_Remove Nth Node From End of List

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41778305 Given a linked list, ...

LeetCode Remove Nth Node From End of List 删除倒数第n个元素

这里的Note的意思应该就是可以不做特殊处理n值了。不过其实特殊处理也就一句语句。 思路: 快指针和慢指针的概念,先一个指针先走,到了离起点n个节点远的时候,慢指针走,等到快指针走到结尾,那么慢指...

【Leetcode】19. Remove Nth Node From End of List

方法一: 思路: (1)若链表为空,直接返回null。 (2)求链表的长度len。 (3)若n==len,则删除的是第一个节点,直接删除后返回即可。 (4)从头开始遍历链表,找到待删除节点cur,并用...

LeetCode OJ 之 Remove Nth Node From End of List ( 删除链表的从尾部数第n个结点 )

题目: Given a linked list, remove the nth node from the end of list and return its head. 删除链表的从尾部数第...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 019 —— Remove Nth Node From End of List
举报原因:
原因补充:

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