实现的代码如下:
//删除链表的中间节点和a/b处的节点
public class DelMidList{
//链表节点的定义
public static class Node{
public int value;
public Node next;
public Node(int data)
{
this.value=data;
}
}
//删除链表中间节点
public static int DelMidOfList(Node head)
{
if(head==null)
{
return Integer.MAX_VALUE; //表示不删除任何节点
}
Node tmp=head;
int leng=0; //记录链表的长度
while(tmp!=null)
{
++leng;
tmp=tmp.next;
}
System.out.println("链表的长度为:"+leng);
if(leng==1)
{
return head.value;
}
if(leng%2==0) //节点偶数处理方式
{
for(int i=0;i<leng/2-2;i++)
{
head=head.next;
}
}
else
{ //节点奇数处理方式
for(int i=0;i<(int)(leng/2)-1;i++)
{
head=head.next;
}
}
Node p=head.next; //要删除的中间节点
head.next=p.next; //删除中间节点
return p.value;
}
//删除链表中a/b处节点
public static int DelABOfList(Node head,int a,int b)
{
double r=(double)a/(double)b; //注意整型比较会向下取整 例如(1/5==0,2/5==0)
//System.out.println("a/b的值为:"+r);
if((head==null)|| (a<0)||(b<0))
{
return Integer.MAX_VALUE; //表示没有可删除的节点
}
if((r==0.0)||(r>1.0))
{
return Integer.MAX_VALUE; //表示没有可删除的节点
}
Node tmp=head;
int len=0; //记录链表的长度
while(tmp!=null)
{
++len;
tmp=tmp.next;
}
System.out.println("链表的长度为:"+len);
//删除第一个节点
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=head.next;
}
}
Node p=head.next; //要删除的中间节点
head.next=p.next; //删除中间节点
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("删除中间节点值: "+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("删除链表的a/b的值为:"+DelABOfList(mode,1,5));
}
}
左神的代码:
public class Problem_03_RemoveNodeByRatio {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
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);
}
}