package main.algorithm.leetcode;

import org.junit.Test;

/**
 * Title : main.algorithm.leetcode <br>
 * Description :
 *   <P> Runtime: 0 ms, faster than 100.00% of Java online submissions for Remove Nth Node From End of List.
 *   <P> Memory Usage: 34.8 MB, less than 100.00% of Java online submissions for Remove Nth Node From End of List.
 * @author chile
 * @version 1.0
 * @date 2019/8/21 11:21
 */
public class RemoveNthFromEnd {

    public class ListNode {

        int val;

        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            ListNode tempNode = head, prevNode = null, nextNode = null;

            int length = 0;
            while (tempNode != null) {
                length++;
                tempNode = tempNode.next;
            }

            int index = length - n;

            if (index == 0) {
                return head.next;
            } else {
                tempNode = head;
                for (int i = 0; i < length; i++) {
                    if (i == index - 1) {
                        prevNode = tempNode;
                    }
                    if (i == index) {
                        prevNode.next =  tempNode.next;
                        break;
                    }
                    tempNode = tempNode.next;
                }
            }

            return head;
        }
    }

    private ListNode createList() {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;

        return n1;
    }

    @Test
    public void test1() {

        for (int i = 1; i < 6; i++) {
            System.out.print("nth is: " + i + ", result: ");
            ListNode head = new Solution().removeNthFromEnd(createList(), i);
            while (head != null) {
                System.out.print(head.val);
                head = head.next;
            }
            System.out.println();
        }
    }

}

@@ -27,16 +27,111 @@
 tcode;%0A%0A
+import main.algorithm.leetcode.structure.ListNode;%0Aimport main.algorithm.leetcode.util.LCUtil;%0A
 import o
@@ -145,16 +145,16 @@
 t.Test;%0A
-
 %0A/**%0A * 
@@ -530,143 +530,8 @@
  %7B%0A%0A
-    public class ListNode %7B%0A%0A        int val;%0A%0A        ListNode next;%0A%0A        ListNode(int x) %7B%0A            val = x;%0A        %7D%0A    %7D%0A%0A
     
@@ -1422,355 +1422,8 @@
  %7D%0A%0A
-    private ListNode createList() %7B%0A        ListNode n1 = new ListNode(1);%0A        ListNode n2 = new ListNode(2);%0A        ListNode n3 = new ListNode(3);%0A        ListNode n4 = new ListNode(4);%0A        ListNode n5 = new ListNode(5);%0A%0A        n1.next = n2;%0A        n2.next = n3;%0A        n3.next = n4;%0A        n4.next = n5;%0A%0A        return n1;%0A    %7D%0A%0A
     
@@ -1614,16 +1614,23 @@
 FromEnd(
+LCUtil.
 createLi
@@ -1631,16 +1631,17 @@
 eateList
+5
 (), i);%0A

use util do the repetitive work