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

Remove Nth Node from End of List

2013-05-15 04:14 363 查看
/**
* 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 == null)
return null;
int i = 0;
ListNode tail = head;
ListNode node = head;
while(i++ < n){
tail = tail.next;
}
if(tail == null)
return head.next;
while(tail.next != null){
tail = tail.next;
node = node.next;
}
node.next = node.next.next;
return head;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: