Joseph problem of circular single linked list __ single linked list

Source: Internet
Author: User
Tags static class


Solve the Joseph problem public class yuesefu{//define the nodes of the linked list public static class node{public int value;

      Node Next;
      public Node (int data) {this.value=data; }///Solve the Josephson problem/** head Ring List header node num reported number/public static node Yuesefu (node Head,int num) {if (head==null| | num<=0| |
    	Head.next==head) {return head;
        	//When the count is 1 o'clock, it must be the last number if (num==1) {Node p=head;
        		while (head.next!=p) {Node q=head;
        		Head=head.next;  Q=null;
        Deletes a linked list of nodes} return head; int t=0;
           The location where the record list is reached while (head.next!=head)//When not the last node {++t;
            if (t==num-1)//delete nodes {node P=head.next each time the number of reported nodes is Num;
            Head.next=head.next.next;
            P=null;
           t=0;
    	} Head=head.next;

    return head; /** advanced: When the ring list length is n, in O ((N) Complexity complete operation */public static node Nyuesefu (Node head,int num) {if head==null| | num<=0| |
    	Head.next==head) {return head;
        	//When the count is 1 o'clock, it must be left for the last number if (num==1) {Node p=head;
        		while (head.next!=p) {Node q=head;
        		Head=head.next;  Q=null;
        Deletes a linked list of nodes} return head;
        int leng=1;
        Node P=head;
        	while (P.next!=head) {++leng;
        P=p.next;

        } leng=getlive (Leng,num);

        while (--leng!=0) {head=head.next;
        }//Last node Head.next=head;

    return head;
         ///Get new Old Joseph node relationship (Num (i) with num (i-1)) public static int getlive (int len,int num) {if (len==1)
         {return 1;
    Return (Getlive (len-1,num) +num-1)%num+1;
        public static void Main (String []args) {//system.out.println ("Hello"); Tectonic Joseph Ring (1->2->3-&Gt;4->1 node Node=new node (1);
		Node.next=new Node (2);
		Node.next.next=new Node (3);
        Node.next.next.next=new Node (4);

       Node.next.next.next.next=node;
       Node Mode=yuesefu (node,1);

        System.out.println (Mode.value);
        Node Tnode=nyuesefu (node,2);

	System.out.println (Tnode.value);
 }
      
}


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.