LeetCode Remove Nth Node From End of List
来源:互联网 发布:淘宝卖家用什么app聊天 编辑:程序博客网 时间:2024/06/05 20:12
Description:
Given a linked list, remove the nth node from the end of list and return its head.
Solution:
First get the length of the whole list, then we need to take out the (length-n)th one.
We can get the (length-n-1)th node as node tot, then get next node node next. Use the following code to get the next node out:
tot.next=next.next;
next.next=null;
Pay attention to the condition when the length==n.
public class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {int length = length(head);ListNode tot = head;if (n == length) {tot = head.next;head.next = null;head = tot;} else {for (int i = 0; i < length - n - 1; i++)tot = tot.next;ListNode next = tot.next;tot.next = next.next;next.next = null;}return head;}int length(ListNode head) {int length = 0;ListNode tot = head;while (tot != null) {length++;tot = tot.next;}return length;}}
0 0
- LeetCode[Linked List]: Remove Nth Node From End of List
- 19. Remove Nth Node From End of List leetcode list
- [Leetcode]LeetCode 19:Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- [Leetcode] Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- leetcode 40: Remove Nth Node From End of List
- [LeetCode] Remove Nth Node From End of List
- Leetcode 19 Remove Nth Node From End of List
- [Leetcode] Remove Nth Node From End of List
- [LeetCode]Remove Nth Node From End of List
- LeetCode-Remove Nth Node From End of List
- [LeetCode] Remove Nth Node From End of List
- LeetCode 19: Remove Nth Node From End of List
- LeetCode - Remove Nth Node From End of List
- LeetCode | Remove Nth Node From End of List
- leetcode之Remove Nth Node From End of List
- 开启Windows 8自带Hyper-V功能创建虚拟机
- Sublime的SVN插件
- PHP设计模式 五 (观察者 策略 装饰器模式)
- 杭电1212 Big Number (大数求模运算)
- Android的5大布局
- LeetCode Remove Nth Node From End of List
- 直方图均衡化matlab实现(续)
- Window卸载Java出错:无法访问windows installer服务
- Andorid--AsyncHttpClient(二)
- OpenCV3.0编译安装
- android开发之Android应用性能评测调优
- input只能输入文本框
- S7-300 PLC的5种定时器
- Esper入门简介:四、 由上篇三我们实现个简单的报警作用吧