本文是在学习中的总结,欢迎转载但请注明出处: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. 浅谈JS变量声明和函数声明提升

    先来两个问题 很多时候,在直觉上,我们都会认为JS代码在执行时都是自上而下一行一行执行的,但是实际上,有一种情况会导致这个假设是错误的. a = 2; var a; console.log(a); 按 ...

  2. Node.js 虚拟机

    稳定性: 3 - 稳定 可以通过以下方法访问该模块: var vm = require('vm'); JavaScript 可以立即编译立即执行,也可以编译,保存,之后再运行. vm.runInThi ...

  3. django模板语言中的extends,block和include

    extends和block一起用 它们用于母版和子版的继承 在母版html中将一些需要替换的部分用{% block xxx %}...{% endblock %}括起来, 在子版html中,在第一行需 ...

  4. 实验与作业(Python)-03 Python程序实例解析

    截止日期: 要求: 下周实验课前上交,做好后在实验课上检查可获取平时分. 做出进阶或选做的的请用清晰的标致标识出来,方便老师批改 本次作业:可提交也可不提交.作业算平时成绩. 本次作业内容量较大,请组 ...

  5. MacOS下Rails+Nginx+SSL环境的搭建(中)

    三.配置Nginx 先是修改 hosts 文件,意思是创建一个本地域名以便我们访问,比如: $ sudo subl /etc/hosts 127.0.0.1 rails_project.local 但 ...

  6. 23 服务的启动Demo2

    MainActivity.java package com.qf.day23_service_demo2; import android.app.Activity; import android.co ...

  7. C语言如何在两个文件中访问同一个全局变量

    方法一: 不使用头文件. 1.c 中 int var; 2.c 中 extern int var; 方法二: 使用头文件. 1.c 中 int var; 不必添加#include "1.h& ...

  8. Android Multimedia框架总结(十四)Camera框架初识及自定义相机案例

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52738492 前言:国庆节告一段 ...

  9. springMVC源码分析--HandlerInterceptor拦截器(一)

    对SpringMVC有所了解的人肯定接触过HandlerInterceptor拦截器,HandlerInterceptor接口给我们提供了3个方法: (1)preHandle: 在执行controll ...

  10. BeanUtils Exception 之 FastHashMap

    这里仅仅是为了记录一件十分奇怪的事情,在使用BeanUtils的过程中,所有的依赖包都添加了, common logging common collections ··· 在为boolean 这种基本 ...