19. Remove Nth Node From End of List
2018-03-11 10:50
375 查看
关于链表尾部节点的问题可以考虑快慢节点的方法解决。
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode start = new ListNode(0);
ListNode slow = start, fast = start;
slow.next = head;
//Move fast in front so that the gap between slow and fast becomes n
for(int i=1; i<=n+1; i++) {
fast = fast.next;
}
//Move fast to the end, maintaining the gap
while(fast != null) {
slow = slow.next;
fast = fast.next;
}
//Skip the desired node
slow.next = slow.next.next;
return start.next;
}
相关文章推荐
- 19. Remove Nth Node From End of List
- leetcode - 19. Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- [LeetCode-Java]19. Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List(删除链表)
- 19. Remove Nth Node From End of List
- 【LeetCode】19 - Remove Nth Node From End of List
- [leetcode]19. 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 [Difficulty: Medium]
- 【19】Remove Nth Node From End of List
- [LeetCode 19] Remove Nth Node From End of List Solutions
- LeetCode-19-Remove-Nth-Node-From-End-of-List 链表水题
- 19. Remove Nth Node From End of List
- leetcode题解-19. Remove Nth Node From End of List
- 19. 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 移除链表倒数第N个节点
- 19 Remove Nth Node From End of List
- leetcode_19题——Remove Nth Node From End of List(链表)