LeetCode——Remove Nth Node From End of List
来源:互联网 发布:mysql存储过程怎么用 编辑:程序博客网 时间:2024/05/22 08:10
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.
题目:给定一个链表,从尾部删除第n个节点并返回新的链表。
思路:使用两个指针,fast 和 slow,他们的距离是n,于是fast到尾的时候,n所在的节点就是需要删除的节点。
public ListNode removeNthFromEnd(ListNode head, int n) {ListNode slow = head, fast = head;if (head.next == null)return null;for (int i = 1; i <= n; i++)slow = slow.next;if (slow == null) {head = head.next;return head;}while (slow.next != null) {slow = slow.next;fast = fast.next;}fast.next = fast.next.next;return head;} // Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } }
0 0
- LeetCode——Remove Nth Node From End of List
- Remove Nth Node From End of List —— Leetcode
- leetcode—Remove Nth Node From End of List
- LeetCode19——Remove Nth Node From End of List
- <19>——Remove Nth Node From End of List
- Algorithms—19.Remove Nth Node From End of List
- 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]LeetCode 19:Remove Nth Node From End of List
- leetcode——19——Remove Nth Node From End of List
- Python学习——leetcode(Remove Nth Node From End of List)
- leetcode 019 —— 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 OJ刷题历程——Remove Nth Node From End of List
- LeetCode——19. Remove Nth Node From End of List
- ZOJ-1203
- JavaScript中获取当前项目的绝对路径
- 国家气象局提供的天气预报接口(完整Json接口)
- 数据结构_求二叉树的高度以及层次遍历二叉树算法_C语言源代码
- dagger注入在eclipse下的配置。
- LeetCode——Remove Nth Node From End of List
- mygui初探(四)自定义ui控件
- windows7下vs2012+qwt6.1+qt5.3.1的配置注意问题。
- OC习题
- C#datagridview导出Excel报表
- AutoHotkey版Warkey编制
- 【北大夏令营笔记-数学题】百练1700-Crossing River
- 7.20_java学习_异常
- poj3164 Command Network 最小树形图-朱刘算法