1class Solution {2public ListNode removeNthFromEnd(ListNode head, int n) {3ListNode temp = head;4int len = 0;56if (head == null || head.next == null) return null;78while (temp != null) {9temp = temp.next;10len++;11}1213if (len == n) return head.next;1415int frontlen = len - n - 1;1617ListNode first = head.next;18ListNode second = head;1920int count = 0;2122while (first != null) {23if (count == frontlen) {24second.next = first.next;25break;26} else {27first = first.next;28second = second.next;29count++;30}31}3233return head;34}35}