Beats 100% of the Java Solutions


  • -10
    C

    public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
    if (head == null) {
    return head;
    }
    if (head.next == null && n == 1) {
    return null;
    }
    ListNode firstPtr = head;
    ListNode secondPtr = head;
    ListNode ret = head;
    while (firstPtr.next != null) {
    firstPtr = firstPtr.next;
    if (n > 0) {
    n--;
    } else {
    secondPtr = secondPtr.next;
    }
    }
    if (n > 0) {
    ret = ret.next;
    secondPtr = null;
    } else if (secondPtr.next == null) {
    secondPtr = null;
    } else {
    secondPtr.next = secondPtr.next.next;
    }
    return ret;
    }
    }


  • 1
    B

    Jesus that's impossible to read. Try formatting...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.