Delete the penultimate n node of the list

Keywords: Mobile github Java

Java code implementation: Delete the penultimate node of the list

Problem Description:
Give you a one-way list, delete the penultimate N node of the list, and return to the head node. The number n here is a valid number.

Given linked list: 1->2->3->4->5, and n = 2.

After removing the penultimate node: 1 - > 2 - > 3 - > 5.

Method 1: First traverse to get the length of the list, then get the first element to be removed, modify the element node. next - > node. next. next.

The code is as follows:

    /**
     * Remove the last n node of the list, first traverse to get the length of the list, then get the first element to be removed, modify the node. next - > node. next. next of the element.
     *
     * @param head
     * @param n
     * @return
     */
    public SingleNode removeNthFromEnd1(SingleNode head, int n) {
        SingleNode dummy = new SingleNode(0, head);

        //Get the length of the linked list
        int length = 0;
        SingleNode first = head;
        while (first != null) {
            length++;
            first = first.next;
        }

        // Find the node labeled length - N - 1 and let its next point point down.
        first = head;
        int index = 0;
        while (index < length - n - 1) {
            first = first.next;
            index++;
        }
        first.next = first.next.next;
        return dummy.next;
    }

The test code is as follows:

        SingleLinkedList sll = new SingleLinkedList();
        for (int i = 0; i < 5; i++) {
            sll.addLast(i + 1);
        }
        System.out.println(sll.toString());
        SingleNode node1 = removeNthFromEnd1(sll.getFirst(), 2);
        sll.logFromHead("removeNthFromEnd1", node1);

The output is as follows:

I/System.out: SingleLinkedList:[1, 2, 3, 4, 5]
I/System.out: removeNthFromEnd1:[1, 2, 3, 5]

Interested students can modify test cases themselves.

Method 2: Use two pointers, two pointers keep a fixed distance n+1, and then start traversal. When the current pointer points to null, the latter pointer just points to the previous one of the nodes to be removed. Let's just let its next point to its next.next.

    /**
     * Remove the reciprocal nth node of the list and use two pointers to implement it.
     *
     * @param head
     * @param n
     * @return
     */
    public SingleNode removeNthFromEnd2(SingleNode head, int n) {
        SingleNode dummy = new SingleNode(0, head);
        SingleNode left = dummy;
        SingleNode right = dummy;
        for (int i = 0; i <= n; i++) {
            right = right.next;
        }
        while (right != null) {
            left = left.next;
            right = right.next;
        }
        left.next = left.next.next;
        return dummy.next;
    }

The test code is as follows:

        SingleLinkedList sll = new SingleLinkedList();
        for (int i = 0; i < 5; i++) {
            sll.addLast(i + 1);
        }
        System.out.println(sll.toString());

        SingleNode node2 = removeNthFromEnd2(sll.getFirst(), 2);
        sll.logFromHead("removeNthFromEnd2", node2);

The output results are as follows: consistent with expectations.

    SingleLinkedList:[1, 2, 3, 4, 5]
    removeNthFromEnd2:[1, 2, 3, 5]

See the complete code

Search for Single LinkedList in the project.
github portal https://github.com/tinyvampirepudge/DataStructureDemo

gitee portal https://gitee.com/tinytongtong/DataStructureDemo

Reference resources:
1,remove-nth-node-from-end-of-list

2,Use Java to implement one-way linked list, and complete list inversion.

Posted by leeming on Wed, 23 Jan 2019 09:18:13 -0800