Lintcode: Nth to Last Node in List
2023-09-11 14:14:07 时间
Find the nth to last element of a singly linked list. The minimum number of nodes in list is n. Example Given a List 3->2->1->5->null and n = 2, return node whose value is 1.
Runner Technique: 两个指针都从Dummy node出发,结束条件是runner.next!=null
1 public class Solution { 2 /** 3 * @param head: The first node of linked list. 4 * @param n: An integer. 5 * @return: Nth to last node of a singly linked list. 6 */ 7 ListNode nthToLast(ListNode head, int n) { 8 // write your code here 9 ListNode dummy = new ListNode(0); 10 dummy.next = head; 11 ListNode walker = dummy; 12 ListNode runner = dummy; 13 while (runner.next != null && n>0) { 14 runner = runner.next; 15 n--; 16 } 17 while (runner.next != null) { 18 runner = runner.next; 19 walker = walker.next; 20 } 21 return walker.next; 22 } 23 }
相关文章
- java 基础之 list
- Java8中list转map
- List根据对象的两个字段进行排序,并且有一个倒序
- 第十四节,基本数据类型,列表list
- Python中实现对list做减法操作介绍
- [WASM] Run WebAssembly in Node.js using the node-loader
- [Immutable.js] Differences between the Immutable.js Map() and List()
- [Whole Web, Node.js, PM2] Restarting your node.js app on code change using pm2
- [Node.js] Using npm link to use node modules that are "in progress"
- [WASM] Run WebAssembly in Node.js using the node-loader
- [Node.js] node-persist: localStorage on the server
- [Node.js] Using npm link to use node modules that are "in progress"
- json串 转 list<class> 方法 List转JSONArray和JSONArray转List String 转List
- sap.m.list render initialization process
- 如何在 SAP CRM WebClient UI 里在 Context node 上下文里访问其他 Context Node 的数据
- list移除null元素 java
- leetcode 题解 || Remove Nth Node From End of List 问题
- Java List详解
- k8s list
- [LeetCode] Delete Node in a Linked List
- 爬虫学习之-Python list 和 str 互转