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 ...
随机推荐
- url重定向或者重写
有四种方式:1.urlMappings,返回200状态码 <system.web> <urlMappings > <add url="~/others.aspx ...
- Emacs Python 自动补全--Elpy
安装方法: 首先,安装一些依赖包: # Either of these pip install rope pip install jedi # flake8 用来检查语法错误 pip install ...
- CSS相关
===CSS框架=== https://github.com/lucasgruwez/waffle-grid 一个易于使用的 flexbox 栅格布局系统 ===CSS初始化=== https://g ...
- CRM客户关系管理系统(六)
第六章.排序和搜索功能开发 6.1.排序功能开发 (1)kingadmin_tags.py @register.simple_tag def get_sorted_column(column,sor ...
- 小知识点 取消button动作 和取巧按钮高亮
如果让按钮在点击时不变暗 进行下面设置: 如果使butten失去动画效果进行下面设置:(一般用于图片+文字,效果等于可以加图片班的label,当然label不能加图片)
- ROS新功能包PlotJuggler绘图
http://www.ros.org/news/2017/01/new-package-plotjuggler.html PlotJuggler,一个基于Qt的应用程序,允许用户加载,搜索和绘图数据. ...
- self关键字
self关键字 self:当前类/对象的指针(指向当前对象/方法调用者) 作用1 当类里有变量名和成员变量名一样的时候,可以使用self区分 例: 我们写一个人的类,有一个年龄属性,在get方法里,我 ...
- Docker简介/安装/使用
什么是Docker?docker是一个开源的应用容器引擎,系统级的轻量虚拟化技术.应用程序的自动化部署解决方案,能够迅速创建一个容器,并在容器上部署和运行应用程序,并通过配置文件可以轻松实现应用程序的 ...
- Hive的HQL语句及数据倾斜解决方案
[版权申明:本文系作者原创,转载请注明出处] 文章出处:http://blog.csdn.net/sdksdk0/article/details/51675005 作者: 朱培 ID ...
- 剑指Offer——知识点储备-操作系统基础
剑指Offer--知识点储备-操作系统基础 操作系统 操作系统什么情况下会出现死锁? 产生死锁的必要条件 (1)互斥条件:即某个资源在一段时间内只能由一个进程占有,不能同时被两个或两个以上的进程占有, ...