[sword finger Offer] - the last K nodes in the list

Posted by blargalarg on Wed, 13 Nov 2019 19:37:59 +0100

Title Description

Input a list and output the K-th last node in the list. In order to meet the habit of most people, count from 1, that is, the end node of the list is the last one. For example, a linked list has 6 nodes, and their values are 1, 2, 3, 4, 5 and 6 from the beginning. The last node of this linked list is a node with a value of 4.

So the idea of implementation is that according to the given node to be deleted, the next year's node can be found directly, and the content of the later node can be copied to the current node. Meanwhile, the current node points to the later node of the later node to ensure that the chain list is continuously open, and then deleting the next node is equivalent to deleting the given node.

Solving problems

  • Define two pointers. One pointer takes k-1 step first, and then two pointers traverse at the same time until the first pointer points to the tail. Then the position of the previous pointer is K.
Algorithm diagram

Reference code:
package offer;

/**
 * Input a list and output the K-th last node in the list. In order to conform to the habits of most people,
 * Count from 1, that is, the end node of the list is the last one.
 * For example, a linked list has 6 nodes, and their values are 1, 2, 3, 4, 5 and 6 from the beginning. The last node of this linked list is a node with a value of 4.
 */
public class Offer22 {
    public static void main(String[] args) {
        LinkNode<Integer> LinkNode = new LinkNode<Integer>();
        LinkNode<Integer> node2 = new LinkNode<Integer>();
        LinkNode<Integer> node3 = new LinkNode<Integer>();
        LinkNode<Integer> node4 = new LinkNode<Integer>();
        LinkNode<Integer> node5 = new LinkNode<Integer>();
        LinkNode.next = node2;
        LinkNode.node_value = 1;
        node2.next = node3;
        node2.node_value = 2;
        node3.next = node4;
        node3.node_value = 3;
        node4.next = node5;
        node4.node_value = 4;
        node5.node_value = 5;
        node5.next = null;
        int K = 5;
        System.out.println(deleteOnK(LinkNode, K));
    }

    /**
     * Pointer 1 goes first k step pointer 2 is at the start position when 1 goes to the end 2 is k
     *
     * @param linkNode
     * @return
     */
    private static int deleteOnK(LinkNode<Integer> linkNode, int K) {
        if(linkNode==null||K==0){
            return 0;
        }
        LinkNode<Integer> firstK = linkNode;
        LinkNode<Integer> last = null;
        for (int i = 0; i < K - 1; i++) {
            if(firstK.next==null){
                return 0;
            }
            firstK = firstK.next;
        }
        last=linkNode;
        while (firstK.next != null) {
            last = last.next;
            firstK = firstK.next;
        }
        return last.node_value;
    }
}

/**
 * Data structure of analog linked list
 *
 * @param <E>
 */
class LinkNode<E> {
    LinkNode<Integer> next;
    E node_value;
}
appendix

The source code of this question is in my Github Above!

Topics: github