The algorithm summarizes the nodes that delete the middle node of the list and A/b node (the important idea of the middle node of the linked list)

Source: Internet
Author: User

Table header node for given list head, implementing function to delete the middle node of the linked list

Promotion: The head node of a given list, integer A and integer b, implements a function to delete a/b node

First to analyze the original problem,

Length 1 Direct return

Length 2 Remove the head node

Length 3 Remove the second length 4 remove the second length 5 remove the third one ... Length each additional 2 deleted nodes move back one node

If you want to delete a node, you need to find the previous node of the node to be deleted

Package Tt;public class Test87 {public class Node{public int value;public node next;public node (int data) {this.value = dat A;}} 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;}}

  

The formula is calculated as Double R = ((double) (a*n))/((double) b) n is the length of the linked list is rounded up and then the first node of the node is found to be deleted.

 PackageTT;ImportTT. Test85.node; Public classTest87 { PublicNode Removebyratio (node head,intAintb) {                if(A<1 |a >b) {            returnHead//do not delete any nodes        }        intn = 0; Node cur=Head;  while(cur! =NULL) {n++;//CountCur =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; }        returnHead; }        }

The algorithm summarizes the nodes that delete the middle node of the list and A/b node (the important idea of the middle node of the linked 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.