Leetcode_19_Remove Nth Node From End of List
本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章
- [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 ...
- 【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 ...
- 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, ...
- 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, ...
- 【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 ...
- 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 ...
- 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 ...
- [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, ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
随机推荐
- FileOutputStream&FileInputStream&异常的使用
FileOutputStream&FileInputStream&异常的使用 我们总觉得历史是极其遥远的东西,与我们并无关联,又觉得历史隐藏在图书馆的旧书之中. 然而,我们每个人都有真 ...
- linux 3.10 串口注册
这个调用过程特别奇特,值得记下来. 最外层调用start_kernel的console_init()进行串口注册. console_init()调用drivers/tty/tty_io.c: void ...
- python笔记九(迭代)
一.迭代 通过for循环来遍历一个列表,我们称这种遍历的方式为迭代.只要是可迭代对象都可以进行迭代操作. 以下代码可以用来判断一个对象是否是可迭代的. 一类是集合数据类型,如list.tuple.di ...
- Rstudio-server 重新登录载入session的问题
Rstudio-server 非常好用,但是免费版的也有一些问题,一个是只能支持在一个客户端的登录,另一个就是每次登录都要导入之前session的问题,对于第二个问题的解决方案,我们可以进入到~/.r ...
- MongoDB 复制(副本集)
MongoDB复制是将数据同步在多个服务器的过程. 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性. 复制还允许您从硬件故障和服务中断中恢复数据. ...
- Git幕后的“故事”
因为做操作系统实验的原因,所以通读了一遍<Understanding git conceptually>,觉得确实不错,于是就简单地记录一下.有的地方理解的还不是很深,可能不够准确,等抽时 ...
- 安卓高级Fresco图片框架的时候
Fresco:2015FaceBook推出的 及其强大 支持webp图片格式 和渐进式图片加载 中文文档 使用方法 引入依赖 点击查看具体教程 基本使用步骤 在布局中使用其标签 <com.fac ...
- iOS开源加密相册Agony的实现(七)
简介 虽然目前市面上有一些不错的加密相册App,但不是内置广告,就是对上传的张数有所限制.本文介绍了一个加密相册的制作过程,该加密相册将包括多密码(输入不同的密码即可访问不同的空间,可掩人耳目).Wi ...
- 带有进度条的WebView
带有进度条的WebView 本篇继于WebView的使用 效果图 自定义一个带有进度条的WebView package com.kongqw.kbox.view; import android.con ...
- [OpenCV]在显示窗口中截图
[OpenCV]在显示窗口中截图 简介 介绍使用OpenCV实现简单的截图功能.首先阐述实现此功能的基本步骤,然后给出实现代码,最后贴出实验结果以及遇到的问题. 基本步骤 我们需要知道OpenCV使用 ...