19Remove Nth Node From End of List
来源:互联网 发布:淘宝代做毕业设计真假 编辑:程序博客网 时间:2024/05/01 15:36
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null) return null;
ListNode res = head;
int len = 0;
while(res!=null){
res = res.next;
++len;
}
if(len < n) return null;
if(len==n) return head.next;
res = head;
for(int i=0;i<len-n-1;++i){
res = res.next;
}
res.next = res.next.next;
return head;
}
}
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null) return null;
ListNode res = head;
int len = 0;
while(res!=null){
res = res.next;
++len;
}
if(len < n) return null;
if(len==n) return head.next;
res = head;
for(int i=0;i<len-n-1;++i){
res = res.next;
}
res.next = res.next.next;
return head;
}
}
0 0
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node from End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- xcode 6 添加pch文件
- OC基础回顾(十二)协议
- FTPClient上传文件乱码
- 虚数的意义
- Log4net终结版
- 19Remove Nth Node From End of List
- Linux 命令学习(1)
- python 获取exception 名字
- play365sdk login.xml编译问题解决
- SNMP从入门到开发:进阶篇 2
- 手机即使战略
- error C2668: “fabs”: 对重载函数的调用不明确
- extjs 可编辑numberfield 控制小数位数
- BZOJ 2242 [SDOI2011]计算器 BSGS+快速幂+EXGCD