[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Orio77/LeetCode/master/RemoveNthNode.java [Back]  [Original]

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        int length = findLength(head);
        int i = 0;
        int traverseTill = length - n - 1;
        if (traverseTill == -1) {
            return head.next;
        }

        ListNode curr = head;
        while (i < traverseTill) {
            curr = curr.next;
            i++;
        }

        curr.next = curr.next.next;
        return head;
    }

    public int findLength(ListNode head) {
        int count = 0;
        if (head == null) {
            return count;
        }
        ListNode curr = head;
        while (curr != null) {
            count++;
            curr = curr.next;
        }

        return count;
    }
}

Web Proxy Viewer  |  New URL  |  Original Page