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

LeetCode 19. Remove Nth Node From End of List

2016-11-25 17:02 295 查看
/**
* 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) {
List<ListNode> list = new LinkedList<ListNode>();
list.add(head);
ListNode node = head.next;
int count = 1;
while (node != null) {
list.add(node);
if (count == n + 1) list.remove(0);
else count++;
node = node.next;
}
if (count == 1) return null;
else if (count == n) return head.next;
else if (list.size() >= 3) list.remove(0).next = list.remove(1);
else list.remove(0).next = null;
return head;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: