/**
* 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;
}
}
Remove Nth Node from End of List
最新推荐文章于 2020-08-05 23:20:29 发布