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)