Remove Nth Node From End of List leetcode java
来源:互联网 发布:php unset函数 编辑:程序博客网 时间:2024/05/17 08:27
视频讲解 http://v.youku.com/v_show/id_XMTY1MTMzNjAyNA==.html
(1)定义两个指针
ListNode fast = head;
ListNode slow = head;
(2)将快指针向前移动N步
(3.1)判断此时快指针是否已经到达尽头,如果是,头节点就是要删除的节点,返回head.next。
(3.2)将快慢两个指针同时以相同的速度往前移动,当快指针走到尽头的时候,慢指针的下一个位置就是倒数第N个节点,将慢指针next指向next.next.
public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode fast = head; ListNode slow = head; for(int i=0;i<n;i++){ fast = fast.next; } if(fast == null){ head = head.next; return head; } while(fast.next != null){ fast = fast.next; slow = slow.next; } slow.next = slow.next.next; return head; }}
0 0
- [Leetcode] Remove Nth Node From End of List (Java)
- [LeetCode][Java] Remove Nth Node From End of List
- leetcode oj java Remove Nth Node From End of List
- LeetCode 19 : Remove Nth Node From End of List (Java)
- LeetCode : Remove Nth Node From End of List [java]
- leetcode:Remove Nth Node From End of List 【Java】
- Leetcode:19. Remove Nth Node From End of List(JAVA)
- Remove Nth Node From End of List leetcode java
- LeetCode – Remove Nth Node From End of List (Java)
- [LeetCode-Java]19. Remove Nth Node From End of List
- [LeetCode] 19. Remove Nth Node From End of List java
- leetcode Remove Nth Node From End of List(Java)
- Remove Nth Node From End of List leetcode java
- 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: Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- Android--Uri
- 数据结构理论知识
- Service的使用(二)启动服务与IntentService
- 杭电Problem 1872 稳定排序
- Qt qml 实现类似手机电话薄联系人首字母查找
- Remove Nth Node From End of List leetcode java
- 7/20android培训第10天
- HDOJ 4883 TIANKENG’s restaurant(思维)
- Jenkins入门系列--Jenkins安装与配置
- uva 11572 唯一的雪花(滑动窗口)
- 漫步微积分八——多项式求导
- 欢迎使用CSDN-markdown编辑器
- Groovy学习
- 几种中文乱码的解决方法