this article illustrates how PHP gets the inverse number K node in the list. To share with you for your reference, it is as follows:

problem

input a linked list, output the countable K node in the linked list. The

solution

notes that this topic is a return node, not a return value. The return value can be stored with a stack. The return node does not do this.

sets two pointers, which first lets the first pointer move k-1 times. Then the two pointer moves at the same time, and when the first pointer reaches the last node, the second pointer is on the countdown K node. Note: K

boundary length may exceed the length of the list, so when the first pointer next is empty, return to the null

 code < PHP /*class ListNode{var $val? VaR; $next = NULL; function __construct ($x) {$this-> Val = $x;}}*/ function FindKthToTail ($head, $k) {if ($head = = NULL $k ==0 return NULL ||); $pre = $head; $last = $head; for ($i=1; $i< $k; $i++) {if ($last-> next = = NULL) return NULL else; $last = $last-> next;} while ($last-> next! = NULL) {$pre = $pre-> $last = next; $last-> next;} return $pre;} 

more PHP on the relevant content of interested readers can see the special station: "PHP data The structure and algorithm of , "php ", summarizes the algorithm program design" php string (string) "PHP array (Array) technique, "PHP common traversal algorithm with and "PHP

"math skills summary

hope the PHP program designed to help to everyone.

This paper fixed link:http://www.script-home.com/php-gets-the-method-of-the-countdown-k-node-in-the-list.html | Script Home | +Copy Link

Article reprint please specify:PHP gets the method of the countdown K node in the list | Script Home

You may also be interested in these articles!