leetcode: Remove Nth Node From End of List
来源:互联网 发布:网络维护培训课程 编辑:程序博客网 时间:2024/04/29 19:16
设计链表的问题(循环,删除)之类的,都是设一个快指针,一个慢指针
快指针比慢指针快n步,注意考虑(1,2) 1和(1,2) 2的边界情况
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {ListNode pFast=head;
ListNode pSlow=head;
ListNode tmp=null;
int diff=0,isMove=0;
while(pFast.next!=null)
{
pFast=pFast.next;
diff++;
if(diff==n)
isMove=1;
if(diff>n)
pSlow=pSlow.next;
}
if(isMove==0)
{
head=head.next;
return head;
}
else
{
tmp=pSlow.next;
pSlow.next=tmp.next;
}
return head;
}
}
0 0
- 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
- [Leetcode] Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- leetcode 40: 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] Remove Nth Node From End of List
- [LeetCode]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 - Remove Nth Node From End of List
- LeetCode | Remove Nth Node From End of List
- leetcode之Remove Nth Node From End of List
- 浅谈卡尔曼滤波器
- [分享]牛牛截图控件最终版
- oralce数据库的用户全介绍
- C与C#的区别
- leetcode JAVA Reverse Words in a String 难度系数2-3?最新题151
- leetcode: Remove Nth Node From End of List
- POJ 2923 Relocation
- Redis的安装配置
- Typecho微语插件-更新至1.01
- .........SDWEBImg
- Sublime Text2.0.2
- java面对象四
- LINUX内核中的xx_initcall初始化标号
- String和StringBuffer的一点研究