Delete the middle node of the list and A/b node __ List

Source: Internet
Author: User

The implementation code is as follows:

Delete the middle node of the list and A/b node public class delmidlist{//List node definition public static class node{public int va
        Lue
        
        Public Node Next;
        public Node (int data) {this.value=data; }///Delete list intermediate node public static int delmidoflist (node head) {if (head==null) {return intege R.max_value;
        Indicates that no nodes are removed} node Tmp=head; int leng=0;
        	Record the length of the list while (tmp!=null) {++leng;
        Tmp=tmp.next;
        } System.out.println ("The length of the linked list is:" +leng);
        if (leng==1) {return head.value; } if (leng%2==0)//node even processing {for (int i=0;i<leng/2-2;i++) {Head=head.nex
          T
        } else {//node odd handling for (int i=0;i< (int) (LENG/2) -1;i++)
        {head=head.next; } Node P=head.next;  The intermediate node head.next=p.next to be deleted;
    Delete Intermediate node return p.value; //Delete List of A/b nodes public static int delaboflist (node head,int a,int b) {double r= double) (Doub  Le) b;

      Note that integer comparisons are rounded down for example (1/5==0,2/5==0)//system.out.println ("A/b value is:" +r); if ((head==null) | | (a<0) | |
      (b<0)) {return integer.max_value;//means there is no node to delete} if (r==0.0) | |
      (r>1.0))
    	
        {return integer.max_value;//means there are no nodes to delete}
        Node Tmp=head; int len=0;
        	Record the length of the list while (tmp!=null) {++len;
        Tmp=tmp.next;
        } System.out.println ("The length of the linked list is:" +len);
        	Deletes the first node if (r==1/(double) len) {node p=head;
        	Head=head.next;
        return p.value; for (int i=2;i<len;i++) {if (r> (double) i/(double) len) {Head=he
       	    Ad.next;  } Node P=head.next;
     The intermediate node to delete  Head.next=p.next;
     Delete Intermediate node return p.value;
		public static void Main (String []args) {//node mode=null;
		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=new Node (5);
		Node.next.next.next.next.next=new Node (6);

		
		System.out.println ("Delete Intermediate node value:" +delmidoflist (node));
		Node Mode=new node (1);
		Mode.next=new Node (2);
		Mode.next.next=new Node (3);
		Mode.next.next.next=new Node (4);
		Mode.next.next.next.next=new Node (5);

	System.out.println ("Delete list's A/b value is:" +delaboflist (mode,1,5)); }
}


Code for Zoshin:

public class Problem_03_removenodebyratio {public static class Node {public int value;

		Public Node Next;
		public Node (int data) {this.value = data;
		The public static node Removemidnode (node head) {if (head = = NULL | | head.next = NULL) {return head;
		} if (Head.next.next = = null) {return head.next;
		Node pre = head;
		Node cur = head.next.next;
			while (Cur.next!= null && cur.next.next!= null) {pre = Pre.next;
		cur = cur.next.next;
		} pre.next = Pre.next.next;
	return head;
		public static node Removebyratio (node head, int a, int b) {if (A < 1 | | a > B) {return head;
		int n = 0;
		Node cur = head;
			while (cur!= null) {n++;
		cur = cur.next;
		} n = (int) Math.ceil ((double) (A * N))/(double) b);
		if (n = = 1) {head = Head.next;
			} if (n > 1) {cur = head;
			while (--n!= 1) {cur = cur.next;
		} cur.next = Cur.next.next;
	return head; } public static void PrintlinkedlIST (Node head) {System.out.print ("linked List:");
			while (head!= null) {System.out.print (Head.value + "");
		head = Head.next;
	} System.out.println ();
		public static void Main (string[] args {node head = new Node (1);
		Head.next = new Node (2);
		Head.next.next = new Node (3);
		Head.next.next.next = new Node (4);
		Head.next.next.next.next = new Node (5);

		Head.next.next.next.next.next = new Node (6);
		Printlinkedlist (head);
		Head = Removemidnode (head);
		Printlinkedlist (head);
		Head = Removebyratio (Head, 2, 5);
		Printlinkedlist (head);
		Head = Removebyratio (head, 1, 3);

	Printlinkedlist (head); }

}


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.