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

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.

思路:

(1) 题意为移除链表倒数第N个元素。

(2) 首先,对链表进行判空,空则返回null(要注意的是还需对N是否在链表大小范围之内进行判断)。

(3) 其次,本文主要用栈来进行操作(这里暂不考虑效率和代码简短问题,仅仅提供一种解题思路)。本文的思想是创建两个栈,一个栈是来存储整个链表中的节点,另一个栈是来存储去除倒数第N个元素后剩余的节点,运用栈先进后出的特性进行操作并得到结果。

(4) 最后,先遍历链表,将链表中节点存入栈s1中;然后,依次从栈s1中pop元素,并设置count来记录pop元素的个数,如果得到count != N,则将pop出的元素加入栈s2中,并将该元素的next设置为空(需要去除节点间的顺序,从栈中pop出时就带有顺序),如果count==N,则说明pop出的元素为待移除的元素,不将其加入s2中;最后,依次从栈s2中pop出元素,将其按pop出的顺序组合起来即为结果。

(5)这里主要考虑到栈的特性:先进后出。能够想到这一点,在不考虑性能等限定的情况下能够很容易解出答案。

算法代码实现如下所示:

public static ListNode removeNthFromEnd(ListNode head, int n) {
	if (head == null)
		return null;

	Stack<ListNode> s1 = new Stack<ListNode>();// 将当前链表所有节点压入栈中
	Stack<ListNode> s2 = new Stack<ListNode>();// 移除指定元素后的栈
	int count = 0;
	while (head != null) {
		s1.push(head);
		head = head.next;
	}

	while (s1.size() != 0) {
		ListNode pop = s1.pop();
		pop.next = null;
		count++;
		if (count == n) {
			continue;
		} else {
			s2.push(pop);
		}
	}

	ListNode result = null;
	ListNode flag = null;
	while (s2.size() != 0) {
		ListNode pop = s2.pop();
		if (result == null) {
			result = pop;
			flag = result;
			continue;
		}
		flag.next = pop;
		flag = flag.next;
	}
	return result;
}

Leetcode_19_Remove Nth Node From End of List的更多相关文章

  1. [LeetCode] 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. For example, Give ...

  2. 【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 ...

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

  4. No.019: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, ...

  5. 【leetcode】Remove Nth Node From End of List(easy)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  6. 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, Give ...

  7. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  8. [leetcode 19] Remove Nth Node From End of List

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

  9. LeetCode: Remove Nth Node From End of List 解题报告

    Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...

随机推荐

  1. ubuntu查看IO

    在命令行直接 cp 一个比较大的文件时,由于没有提示信息,总感觉很不放心,可以通过查看IO的方式确认cp操作的进展程度. 查看IO可以使用iostat命令,但是前提是要安装sysstat sudo a ...

  2. js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题

    js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题 js模拟form表单提交数据源码: /** * js模拟form表单提交 * @param ...

  3. ACM | HDU|6227_Rabbit

          题意:     有n只兔子分别占据不同的位置,任意一只兔子可以插入任意两只兔子的之间,但要求两只兔子之间要有空位,求这样的移动次数最多能够有多少?   在这里每一只兔子没有区别,可以看做把 ...

  4. 用python爬了自己的微信,原来好友都是这样的!

    偶然了解到Python里的itchat包,它已经完成了wechat的个人账号API接口,使爬取个人微信信息更加方便.鉴于自己很早之前就想知道诸如自己微信好友性别比例都来自哪个城市之类的问题,于是乎玩心 ...

  5. jQuery 安装

    网页中添加 jQuery 可以通过多种方法在网页中添加 jQuery. 您可以使用以下方法: 从 jquery.com 下载 jQuery 库 从 CDN 中载入 jQuery, 如从 Google ...

  6. APP自动化框架LazyAndroid使用手册(1)--框架简介

    作者:cryanimal  QQ:164166060 APP自动化简介 APP自动化,即通过自动化的方式,对APP施行一系列的仿按键输入.触摸屏输入.手势输入等操作,以达到对APP的功能进行自动化测试 ...

  7. jboss规则引擎KIE Drools 6.3.0 Final 教程(1)

    前言 目前世面上中文的KIE DROOLS Workbench(JBOSS BRMS)的教程几乎没有,有的也只有灵灵碎碎的使用机器来翻译的(翻的不知所云)或者是基于老版本的JBOSS Guvnor即5 ...

  8. solr多集合配置

    1.1 多SolrCore配置 一个solr工程中可以配置多个SolrCore实例. 分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_ ...

  9. 使用java操作HDFS

    新建Java Project; 1,右击项目,属性,Java Build Path,Libraries,Add External JARs(haddopp根目录下的所以jar): 2,做一下项目关联, ...

  10. Ubuntu和ROS一起愉快玩耍

    Ubuntu和ROS重要的两个中文网址: Ubuntu:http://cn.ubuntu.com/ROS:http://wiki.ros.org/cn Robots and drones on Ubu ...