Comic: How to find the nth node from the bottom of the linked list?

Python combat community

Java combat community

Long press to identify the QR code below, add as required

Scan QR code to follow to add customer service

Enter the Python community▲

Scan QR code to follow to add customer service

Enter the Java community

Author丨Xiao Hui

Source丨Programmer Xiaohui (ID: chengxuyuanxiaohui)


----- the next day -----

What does that mean? Let's take the following linked list as an example:

Given the head node of the linked list, but do not know the actual length of the linked list, we are required to find the nth node from the bottom of the linked list.

Assuming n=3, then the node to be found is element 1:

How to use the queue? Xiao Hui's ideas are as follows:

1. Create a queue of length n, traverse the original linked list, and let the nodes enter the queue one by one:

2. When the queue is full, let the tail element out of the queue, and the new node will enter the queue:

3. When all the nodes in the linked list are traversed, the element at the end of the queue is the nth node from the bottom (because the queue length is n):

————————————

First, we create two pointers P1 and P2, P1 points to the head node of the linked list, and P2 points to the positive nth node of the linked list (that is, the third node in the example):

Next, we let the pointers P1 and P2 move to the right at the same time, one step at a time, until the pointer P2 moves to the end of the linked list:

At this time, since P2 points to the end node of the linked list, and the distance between P1 and P2 is n-1, the node pointed to by P1 is the nth node from the bottom of the linked list we are looking for:

Obviously, this method only needs to traverse the linked list once from beginning to end, and only uses two pointers. The space complexity of the algorithm is O(1).

public class NthFromEnd {
    public static Node findNthFromEnd(Node head, int n){
        Node p1 = head;
        Node p2 = head;
        //把p2指针移动到正数第n个结点
        for(int i=1; i<n; i++){
            p2 = p2.next;
            if(p2 == null){
                throw new IllegalArgumentException("参数n超出链表长度!");
            }
        }
        //p1和p2一起右移,直到p2指向链表尾结点
        while (p2.next != null){
            p1 = p1.next;
            p2 = p2.next;
        }
        return p1;
    }

    //快速创建链表
    private static Node buildLinkList(int[] array){
        Node head = new Node(array[0]);
        Node p = head;
        for(int i=1; i<array.length; i++){
            p.next = new Node(array[i]);
            p = p.next;
        }
        return head;
    }

    //链表节点
    private static class Node {
        int data;
        Node next;

        Node(int data) {
            this.data = data;
        }
    }

    public static void main(String[] args) {
        int[] inputs = {5,3,7,2,4,1,9,8};
        Node head = buildLinkList(inputs);
        Node node = findNthFromEnd(head,3);
        System.out.println("链表倒数第3个元素是:" + node.data);
    }

}


—————END—————

程序员专栏 扫码关注填加客服 长按识别下方二维码进群

Recommended recent exciting content:  

 955. 6 new companies are added to the list of WLB non-overtime companies

 The latest statistics of programmer salaries in October 2020!

 The interviewer asked me how many ways to create threads? I laughed

 Use of python itchat library


Watch the good article here to share it with more people↓↓

Guess you like

Origin blog.csdn.net/Px01Ih8/article/details/109323752