The sword refers to the list of the last K nodes of the---list.

Source: Internet
Author: User

"title" Enter a list to output the last K nodes in the list.

"Idea" Method 3: Set two pointers, the first pointer goes k-1 step, the 2nd pointer starts to walk, (this time the first pointer points to the K) when the first pointer goes to the tail (that is, walk K), the first pointer points to the K-node. (Recommended method)

Method 2: First get the number of nodes, then the number of N-K+1 nodes 1 2 3 4 5 6

Method 1: First get the length, then copy to the array one, and then the reverse order to add to the array 2, this time to output the number of the reciprocal k is the number of the number of numbers in the sequence of 2 k-1. The exception that appears here does not know what to do with it. (My method)

1  PackageCom.exe3.offer;2 3 /**4 * "title" Enter a list to output the last K nodes in the list. 5  * @authorWGS6  *7  */8  Public classPrinklinknode {9     Static classlinknode{Ten         intVal; OneLinknode next=NULL; A          PublicLinknode (intN) { -              This. val=N; -         } the     } -     //Method 3: Set two pointers, the first pointer goes k-1 step, the 2nd pointer starts to walk, (this time the first pointer points to the K) when the first pointer goes to the tail (that is, walk K), the first pointer points to the K-node.  -      PublicLinknode PrintLinkNodeFromTail3 (Linknode node,intk) { -         //exception handling, if the head pointer node is empty or the input K junction is 0 . +         if(node==NULL|| K<=0){ -              +SYSTEM.OUT.PRINTLN ("Invalid"); A             return NULL; at         } -Linknode Firstindex=node;//here to note, highlights -Linknode secondindex=NULL; -          for(inti=0;i<k-1;i++){ -             if(firstindex!=NULL){ -firstindex=Firstindex.next; in}Else{ -                 return NULL; to             } +         } -         //at this point the first pointer has gone to step K thesecondindex=node; *          while(firstindex.next!=NULL){ $firstindex=Firstindex.next;Panax Notoginsengsecondindex=Secondindex.next; -         } the          +         returnSecondindex; A          the     } +     //Method 2: First get the number of nodes, then the number of N-K+1 nodes 1 2 3 4 5 6 -      PublicLinknode PrintLinkNodeFromTail2 (Linknode node,intk) { $Linknode temp=node; $         intLenofnode=0; -         if(node==NULL|| K<=0){ -              theSYSTEM.OUT.PRINTLN ("Invalid"); -             return NULL;Wuyi         } the         //1 getting the length -          while(node!=NULL){ Wulenofnode++;//number of nodes. -Node=Node.next; About         } $         intI=1; -         //2 Shun number up to the first n-k+1 point -          while(K<=lenofnode && (i!=lenofnode-k+1)){ -i++; Atemp=Temp.next; +         } the         //Reasonable -         if(k<=Lenofnode) { $             returntemp; the         } the         return NULL; the          the     }         -     //Method 1: First get the length, then copy to the array one, and then the reverse order to add to the array 2, this time to output the number of the reciprocal k is the number of the number of numbers in the sequence of 2 k-1. The exception that appears here does not know what to do with it.  in      Public intPrintlinknodefromtail (Linknode node,intk) {         theLinknode temp=node; the         intLen=0; About         intM=0; the         //1 getting the length the          while(node!=NULL){ thelen++; +Node=Node.next; -         } the         if(temp==NULL|| k<0| | k>=Len) {Bayi             Try { theSYSTEM.OUT.PRINTLN ("Invalid"); the}Catch(Exception e) { -                 //TODO auto-generated Catch block - e.printstacktrace (); the             } the         } the         int[] arr1=New int[Len]; the          while(temp!=NULL){ -arr1[m++]=Temp.val; thetemp=Temp.next; the         } the         int[] arr2=New int[Len];94          for(inti=0;i<arr1.length;i++){ thearr2[len-1-i]=Arr1[i]; the         } the         if(k<=Len) {98             returnArr2[k-1]; About         } -         return0;101         102         103     }104      Public Static voidMain (string[] args) { thePrinklinknode p=NewPrinklinknode ();106Linknode node1=NewLinknode (1);107Linknode node2=NewLinknode (2);108Linknode node3=NewLinknode (3);109Linknode node4=NewLinknode (4); theLinknode node5=NewLinknode (5);111Linknode node6=NewLinknode (6); thenode1.next=Node2;113node2.next=Node3; thenode3.next=Node4; thenode4.next=Node5; thenode5.next=Node6;117         //3118         /*int N=p.printlinknodefromtail (node1,8);119 System.out.println (n);*/ -         //2121         /*Linknode Node=p.printlinknodefromtail2 (node1,0);122 if (node!=null) {123 System.out.println (node.val);124 }else{ the System.out.println ("null");126         }*/127         //3 -Linknode Node=p.printlinknodefromtail3 (node1,0);129         if(node!=NULL){ the System.out.println (node.val);131}Else{ theSYSTEM.OUT.PRINTLN ("null");133         }134         135     }136     137     138     139      $     141     142     143}

The sword refers to the list of the last K nodes of the---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.