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

Source: Internet
Author: User

Title: Enter a linked list. Output the list of the countdown K-junction.
In order to conform to the habits of most people, the subject starts counting from 1. That is, the tail node of the list is the bottom 1th node.

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

To get the K-node, it is very natural to go first to the end of the list. Then go back from the tail to the K-step.

But we see from the definition of the linked list node is a one-way list of linked list, the node of a one-way list only has a pointer to the back of the past and not from the forward pointer, so this idea does not work.

Since it is not possible to traverse this list from the tail node. We're going to go back to the head node.

If the entire list has n nodes, then the penultimate node is the first n-k-1 node that starts at the beginning of the first node. If we just had to start from the beginning and go back to n-k+1, we'd be suspicious. How do I get the number of nodes n?

This is not difficult, just need to start from the beginning to traverse the list, not through a node, the counter plus 1.

That is to say we need to traverse the list two times, the first time to count the number of nodes in the linked list. The second can be found at the bottom of the 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 list once.

In order to be able to only traverse the list one time to find the bottom of the K node. We can define two pointers. The first pointer starts from the head pointer of the linked list and traverses the 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.

Because the distance of 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.


Many people before the interview from the Internet to see this way with two pointers to traverse the idea to answer this question. So hearing the interviewer's question, they had a happy heart, and wrote the code very quickly. But a few days later it was not an offer but a refusal. So the baffled. In fact the reason is very easy. Is that their code is not robust enough.

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 in the For loop, k-1 steps forward in the linked list. will still cause a crash due to a null pointer.

3, the input of the number of K is 0. or negative, the same will 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 the interviewer to see the code. Finally, he gave a rejection letter rather than an offer.

Below we give the Java version of the Code:

/** * Enter a linked list. Output the list of the countdown K-junction. * In order to conform to most people's habits, the subject starts counting 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 sequentially 1. 2. 3,4,5. 6. The 3rd node of the list is the 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 example; function tests (the K-node is in the middle of the list, and the K-node is the head node of the linked list.) K-node at the end of a linked 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


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.