LeetCode 19 Remove Nth Node From End of List (移除距离尾节点为n的节点)
2017-03-08 12:56
344 查看
题目链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/?tab=Description
Problem: 移除距离尾节点为n的节点.
使用快慢指针,(由于是对链表进行操作,并且是距离尾节点距离为n,因此将快指针移动n个,慢指针移动到fast.next==null即可)
参考代码
Problem: 移除距离尾节点为n的节点.
使用快慢指针,(由于是对链表进行操作,并且是距离尾节点距离为n,因此将快指针移动n个,慢指针移动到fast.next==null即可)
参考代码
package leetcode_50; /*** * * @author pengfei_zheng * 移除距离尾节点为n的节点 */ public class Solution19 { public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public ListNode removeNthFromEnd(ListNode head, int n) { if (head.next == null) { return null; } int counter = 1; ListNode start = new ListNode(0); start.next = head;//保存头指针 ListNode fast = start; ListNode slow = start; while (fast.next != null) {//循环体 fast = fast.next;//快指针移动 if (counter <= n) { counter++;//计数器 } else { slow = slow.next;//慢指针移动 } } slow.next = slow.next.next;//移除 return start.next; } }
相关文章推荐
- 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 移除链表倒数第N个节点
- leetcode:Remove Nth Node From End of List (移除从尾部数的第N个节点)
- 19. Remove Nth Node From End of List (移除链表倒数第n个节点)
- 19. Remove Nth Node From End of List(移除链表的倒数第n个节点)
- [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点
- 【LeetCode题解】19_删除链表的倒数第N个节点(Remove-Nth-Node-From-End-of-List)
- 【Leetcode】19. Remove Nth Node From End of List - 删掉链表倒数第n个节点
- leetcode 19. Remove Nth Node From End of List-移除链表元素|双指针
- leetcode_[python/C++]_19. Remove Nth Node From End of List(删除链表末第n个节点)
- leetcode 3. 移除链表的倒数第n个节点 Remove Nth Node From End of List
- [LeetCode] 19. Remove Nth Node From End of List 删除链表的倒数第N个节点 @python
- leetcode-19. Remove Nth Node From End of List(删除链表倒数第N个节点)
- 【LeetCode-面试算法经典-Java实现】【019-Remove Nth Node From End of List(移除单链表的倒数第N个节点)】
- leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法
- 面试题15:链表中倒数第k个节点(Leetcode-19:Remove Nth Node From End of List)
- [C++]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 (2 solutions)