您的位置:首页 > Web前端 > Node.js

Remove Nth Node From End of List

2013-04-18 16:24 309 查看
/**
* Definition for singly-linked list.
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) {
*         val = x;
*         next = null;
*     }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
// Start typing your Java solution below
// DO NOT write main() function
if(head.next==null)return null;

ListNode last = head;
for(int i = 0; i<n;i++){
last = last.next;
}
if(last==null)return head.next;
ListNode now = head;
while(last.next!=null){
last = last.next;
now = now.next;
}
now.next = now.next.next;
return head;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: