Home » Java » An offer code analysis -- interview questions for ...
The analysis process of

algorithm is in the code annotation:

`/*** Title: enter a single linked list, the output of the chain from the front of the first k number* PS: count from 1 after the next count* @author Chai Maomao big idlers* /Public, class, TailK {/ * ** analysis: to find the last K numbers, it is natural to think of the number k from the end* however, this approach faces two problems: 1., we can't go to the end of the single chain table; 2., we can't go through the single chain table from behind* in order to solve the above two problems, we first think of the method is: traverse two single linked list, first obtain single chain table length N, second times traversal to the first n-k elements can stop* code is as follows:* // / use of global variables result in terms of the operation resultsStatic, Boolean, result = true;/ * ** the first node of the @param first single chain table* the last few elements that @param K is looking for* @return returns the last K value* /Public, static, int, getTailK (Node, first, int, K) {If the list is empty.If (first==null) {System.out.println (the list is empty!);Result = false;Return 0;}/ / if k< = 0If (k<1) {System.out.println ("K can't be less than 1!");Result = false;Return 0;}/ / calculation of chain lengthInt length = 1;Node P = first;While (p.next, =null) {Length++;P = p.next;}If the list is longer than K.If (lengthSystem.out.println ("k=" +k+ "exceeds the length of the linked list");Result = false;Return 0;}/ / linked list traversal, traverse to the first n-k elements of the endNode q = first;For (int i=0; iQ = q.next;Return q.data;}/ * ** the above method can solve the problem, but you need to traverse the list two times. Is there a more efficient way?* you can use two pointers I and j, pointer I, start from scratch, go K, and then J point to the first node, and then keep the distance between I and J. When J goes to the tail, the point I points to is the last K node* code is as follows:* /Public, static, int, getTailK_modify (Node, first, int, K) {If the list is empty.If (first==null) {System.out.println (the list is empty!);Result = false;Return 0;}/ / if k< = 0If (k<1) {System.out.println ("K can't be less than 1!");Result = false;Return 0;}/ / define two pointer in P and Q, P refers to the head node, Q points to the first k nodesNode P = first;Node q = first;The Q / / K nodes to aFor (int i=0; iIf q is not / / to a K node, but q is the last node, the K exceeds the length of listIf (q.next==null) {System.out.println ("k=" +k+ "exceeds the length of the linked list");Result = false;Return 0;}Q = q.next;}//p and Q move backward, respectively, until Q goes to the end of the listWhile (q.next, =null) {P = p.next;Q = q.next;}Return p.data;}/ * ** test* /Public, static, void, main (String[], args) {Node node1 = new, Node ();Node node2 = new, Node ();Node node3 = new, Node ();Node node4 = new, Node ();Node1.data = 1;Node2.data = 2;Node3.data = 3;Node4.data = 4;Node1.next = node2;Node2.next = node3;Node3.next = node4;System.out.println (getTailK_modify (node1,11));}}/ * ** defining nodes* /Class Node{Public T data;Public Node next;}`