本文是在学习中的总结,欢迎转载但请注明出处: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. Node.js Smalloc

    稳定性: 1 - 试验 类: smalloc 由简单内存分配器(处理扩展原始内存的分配)支持的缓存.Smalloc 有以下函数: smalloc.alloc(length[, receiver][, ...

  2. 在Spring Boot中输出REST资源

    前面我们我们已经看了Spring Boot中的很多知识点了,也见识到Spring Boot带给我们的各种便利了,今天我们来看看针对在Spring Boot中输出REST资源这一需求,Spring Bo ...

  3. chrome浏览器不兼容jQuery Mobile问题解决

    最近在学习jQuery Mobile.第一次运行例子的时候发现chrome总是等待,查看后台报错.错误如下所示: 最后在stackoverflow上找到一个解决方案:将以下代码放在 jquery.mo ...

  4. Programming In Scala笔记-第六章、函数式对象

    这一章主要是以定义和完善一个有理数类Rational为线索,分析和介绍有关类定义,构造函数,方法重写,变量定义和私有化,以及对操作符的定义等. 一.Rational类定义和构造函数 1.定义一个空类 ...

  5. Swift中的可选协议和方法的历史渊源

    @objc protocol Transaction { func commit() -> Bool optional func isComplete() -> Bool } 以上协议被标 ...

  6. mysql进阶(二十九)常用函数

    mysql进阶(二十九)常用函数 一.数学函数 ABS(x) 返回x的绝对值 BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制) CEILING(x) 返回大于x的最小整数值 EXP ...

  7. Matlab 2015b 启动时崩溃 MATLAB crashes during startup on Ubuntu 16.04

    Matlab 启动时崩溃 MATLAB crashes during startup on Ubuntu Matlab 2015B Ubuntu 16.04 之前解决过,更新后问题又来了.     出 ...

  8. Linux 环境下的一些常用命令(三)

    转载自 http://www.oschina.net/translate/20-advanced-commands-for-middle-level-linux-users 21. 命令: Find ...

  9. AR模块常用函数

    --AR模块常用函数 FUNCTION get_fnd_user_name ( p_user_id IN NUMBER ) return VARCHAR2 IS CURSOR c_user_name ...

  10. 安卓Button-TextView-EditText综合运用

    1.如何使用安卓中的按键Button? 1.先从控件库拖一个按钮button的控件,在XML设置好宽高等参数 对应的就是Button这个图标,直接拖出来即可; 以下是设置这个按钮对应的XML代码: & ...