Sword Point of Offer (Java Edition): The penultimate K-node in a linked list

Source: Internet
Author: User

Title: Enter a linked list to output the list of the lowest K-points in the list.
In order to conform to most people's habit, the subject starts from 1, that is, the tail node of the list is the 1th node in the bottom.

For example, a list of 6 nodes, starting from the beginning, their values are 1,2,3,4,5,6. The bottom 3rd node of the list is a node with a value of 4.

In order to get the K-node, it is natural to go to the end of the list and back to K-step from the tail. But we see from the definition of the linked list node is the list of links in the list is a one-way list, one-way list of nodes only from the pointer after the back and not from the forward pointer, so this idea does not work.

Since it is not possible to traverse the list from the tail node, we are going back to the head knot. Assuming that the entire list has n nodes, then the penultimate node is the first n-k-1 node that starts at the beginning of the node. If we just start from the beginning and walk backwards, n-k+1 is suspicious. How to get the number of nodes n? This is not difficult, just start from the beginning to traverse the linked list, without a node, the counter plus 1 on the line.

That is to say we need to traverse the list two times, the first time to count the number of nodes in the list, the second time can find the last K node. But when we explain this idea to the interviewer, he will tell us that the solution he expects only needs to traverse the linked list once.

We can define two pointers in order to be able to find the last K-node in a single pass through the list of links. The first pointer starts from the head pointer of the linked list to traverse forward k-1. The second pointer remains motionless; starting with step K, the second pointer also cultures the temple from the head pointer of the linked list to start the traversal. Since the distance of the two pointers remains at K-1, when the first (walking in front) pointer reaches the end point of the list, the second pointer is exactly the last K-node.


A lot of people before the interview from the Internet to see this way with two pointers to traverse the idea to answer this question, so hear the interviewer's question, their hearts a happy, soon wrote the code. But a few days later, is not an offer, but refused to believe, so think not the solution. In fact, the reason is very simple, is that their code is not robust. The interviewer can find 3 ways to break the code.

1. Enter the head pointer null. The program crashes because the code tries to access the memory that the null pointer points to.

2. The total number of nodes in the list with head node is less than K. Because the k-1 step moves forward in the For loop, it can still cause a crash due to a null pointer.

3, the input parameter k is 0, or a negative number, will also cause the program to crash.

With such a simple code there is a risk of a potential crash in 3, and we can imagine what it would be like for an interviewer to see such a code, and ultimately give a rejection instead of an offer.

Below we give the Java version of the Code:

/** * Enter a list to output the list of the lowest K-points in the list. * In order to conform to most people's habits, the subject starts from 1, that is, the tail node of the list is the 1th node in the bottom. * For example, a linked list has 6 nodes, starting from the beginning their values are 1,2,3,4,5,6. The bottom 3rd node of the list is a node with a value of 4 */package swordforoffer;import utils. listnode;/** * @author Jinshuangqi * * August 1, 2015 */public class E15kthnodefromend {public ListNode findkthtotail (listnode Head,int k) {if (head = = NULL | | k <= 0) {return null;} ListNode anode = head; ListNode BNode = null;for (int i = 0;i<k-1;i++) {if (Anode.next! = null) anode = Anode.next;elsereturn null;} BNode = Head;while (Anode.next! = null) {anode = Anode.next; BNode = Bnode.next;} return BNode;} public static void Main (string[] args) {ListNode head = new ListNode (); ListNode second = new ListNode (); ListNode third = new ListNode (); ListNode forth = new ListNode (); head.next = Second;second.next = Third;third.next = Forth;head.data = 1;second.data = 2;th Ird.data = 3;forth.data = 4; E15kthnodefromend test = new E15kthnodefromend (); ListNode result = Test. Findkthtotail (Head,-1); SYSTEM.OUT.PRINTLN (result);}}
Test cases, functional tests (K-nodes in the middle of the list, K-nodes in the head node of the linked list, K-nodes in the tail node of the list)

Special input test: The head node of the linked list is a null pointer, and the head node of the list is less than k,k equals 0.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sword Point of Offer (Java Edition): The penultimate K-node in a linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.