# 第二章 链表问题

## 2.3 删除链表的中间节点和 a/b 处的节点

### 【题目】

**进阶：**给定链表的头节点 head，整数 a 和 b没实现删除位于 a/b 出节点的函数。

### 【实现】

• RemoveMidNode.java
``````public class RemoveMidNode {

private static class Node {
public int value;
public Node next;

public Node(int value) {
this.value = value;
}
}

public RemoveMidNode(int[] arr) {
}

if (arr == null || arr.length == 0) {
return;
}
Node preNode = this.head = new Node(arr[0]);
for (int i = 1; i < arr.length; ++i) {
preNode = preNode.next = new Node(arr[i]);
}
}

public void removeMidNode() {
return;
}
}
while (curNode.next != null && curNode.next.next != null) {
preNode = preNode.next;
curNode = curNode.next.next;
}
preNode.next = preNode.next.next;
}

}
``````
• RemoveByRatio.java
``````public class RemoveByRatio {

private static class Node {
public int value;
public Node next;

public Node(int value) {
this.value = value;
}
}

public RemoveByRatio(int[] arr) {
}

if (arr == null || arr.length == 0) {
return;
}
Node preNode = this.head = new Node(arr[0]);
for (int i = 1; i < arr.length; ++i) {
preNode = preNode.next = new Node(arr[i]);
}
}

public void removeByRatio(int a, int b) {
if (a < 1 || a > b) {
return;
}
return;
}
int n = 1;
while (curNode != null) {
++n;
curNode = curNode.next;
}
n = (int) Math.ceil((double) (a * n) / (double) b);
if (n == 1) {
} else if (n > 1) {
while (--n != 1) {
curNode = curNode.next;
}
curNode.next = curNode.next.next;
}
}

}
``````
• Test.java
``````public class Test {

private RemoveMidNode mid;
private RemoveByRatio ratio;

public Test(int[] arr) {
this.mid = new RemoveMidNode(arr);
this.ratio = new RemoveByRatio(arr);
}

public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
Test test = new Test(arr);
test.mid.removeMidNode();
test.ratio.removeByRatio(3, 5);
}

}
``````