Remove Nth Node From End of List - JS
来源:互联网 发布:网络电子游戏 360 编辑:程序博客网 时间:2024/06/07 19:21
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.
Tag
LinkedList, Two Pointers
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Create two pointers.
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } *//** * @param {ListNode} head * @param {number} n * @return {ListNode} */var removeNthFromEnd = function(head, n) { if(head.next === null || head === null) { return null; } var slow = head, fast = head; while(n>0) { fast = fast.next; n--; } // Remove head situation if(fast === null) { return head.next; } while(fast.next !== null) { slow = slow.next; fast = fast.next; } slow.next = slow.next.next; return head;};
// Make them depart n distance
0 0
- Remove Nth Node From End of List - JS
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node from End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- 欢迎使用CSDN-markdown编辑器
- oracle事务
- NYOJ-768移位密码,最简单的代替密码;
- setTimeout与setInterval 定时器与异步循环数组
- JQ拖曳效果
- Remove Nth Node From End of List - JS
- oracle一个事务的完整流程分析
- oj 圆柱体
- jquery延迟加载插件
- 十一周项目-1-点-圆-圆柱类族的设计(2) (补充)
- 第十四周项目二 两个成员的类模板
- 第十六周程序阅读(4)
- 第15周实践项目1.1-阅读程序,体会STL用法
- 项目2-洗牌(1)