Skip to content

Latest commit

 

History

History
35 lines (31 loc) · 856 Bytes

dev-jonghoonpark.md

File metadata and controls

35 lines (31 loc) · 856 Bytes
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode current = head;
        int length = 0;
        while(current != null) {
            length++;
            current = current.next;
        }

        if (length == 1) {
            return null;
        }

        if (length == n) {
            return head.next;
        }

        int removeIndex = length - n;
        int pointer = 0;
        current = head;
        while (pointer < removeIndex - 1) {
            current = current.next;
            pointer++;
        }
        current.next = current.next.next;
        return head;
    }
}