leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法
来源:互联网 发布:淘宝一件代发赚钱吗 编辑:程序博客网 时间:2024/04/30 01:35
Remove Nth Node From End of List
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.
思路:删除倒数第n个节点,因为是单链表,不知道节点总数,故先遍历,统计节点总数,算出正数第几个,然后删除即可。
算法很简单,代码如下:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { //删除倒数第n个 if(n == 0){ return head; } int nth = 0;//倒数第n个 int count = 0;//总的节点数 ListNode p = head; //统计count while(p != null){ p = p.next; count++; } //计算正数n的值,从0计算 n = count - n; if(n == 0){//如果为0,说明头结点,返回头结点下一个即可 return head.next; } p = head; //数到n-1,然后令n-1.next = n.next = n-1.next.next即可 while(nth < n - 1){ p = p.next; nth++; } p.next = p.next.next; return head; }}
0 0
- leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法
- LeetCode(Remove Nth Node From End of List)删除链表倒数第n个节点
- [LeetCode-19] Remove Nth Node From End of List(删除倒数第N个节点)
- 【LeetCode】Remove Nth Node From End of List 删除链表中倒数第n个节点- Easy +
- leetcode-19. Remove Nth Node From End of List(删除链表倒数第N个节点)
- Remove Nth Node From End of List 删除链表倒数第N个节点
- LintCode Remove Nth Node From End of List 删除链表中倒数第n个节点
- LeetCode 19 Remove Nth Node From End of List 移除倒数第N个节点
- leetcode:Remove Nth Node From End of List(删除链表倒数第n个节点)【面试算法题】
- [LeetCode]—Remove Nth Node From End of List 删除链表的倒数第n个节点
- [C++]LeetCode 19: Remove Nth Node From End of List(删除链表中倒数第n个节点)
- Leetcode #19. Remove Nth Node From End of List 移除倒数第N个链表节点 解题小节
- Remove Nth Node From End of List 链表删除倒数第N个元素@LeetCode
- LeetCode Remove Nth Node From End of List 删除倒数第n个元素
- Leetcode Remove Nth Node From End of List 删除链表倒数第n个元素
- 【Leetcode】19. Remove Nth Node From End of List - 删掉链表倒数第n个节点
- LeetCode 19. Remove Nth Node From End of List--删除链表的倒数第n个结点
- leetcode解题之 19. Remove Nth Node From End of List java 版(删除链表倒数第n个结点)
- WEB_网络_webservice-http-soap的异同_待
- Android设计模式系列--观察者模式
- input标签的placeholder属性
- qt5 cannot find -lGL解决方法
- Android 系统中 Location Service 的实现与架构
- leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法
- unity3d开源Ultimate MMORPG插件技术文档渣翻译第3页
- C++ limits头文件的用法(numeric_limits)
- lua学习总结
- JavaScript的一些要点吧~偶然发现的
- Android设计模式系列--原型模式
- 斐波那契数列算法的优化设计
- 大型网站架构演化
- leetcode Flatten Binary Tree to Linked List树