java找链表中最小值_在单链表中删除最小值结点,写出函数.(JAVA)

展开全部

package testonly;

/**

* 删除单链表中所有最小32313133353236313431303231363533e58685e5aeb931333337373563的节点

* @author baidu an0011121

*

*/

public class LinkedListDemo {

/**

* 链表节点结构,直接在这里表示了,为了贴出来代码,你可以单独把这个节点类放到一个java文件中

* @author Administrator

*

*/

public static class Node {

public int data;

public Node next;

}

/**

* 核心方法,删除单链表中所有的最小的节点

* @param head

*/

public static void delMin(Node head) {

//寻找最小

Node current = head.next;

int min = current.data;

while (null != current) {

if (current.data 

min = current.data;

}

current=current.next;

}

//删除最小

Node previous = head;

current = head.next;

//两层循环为了删除所有最小

while(null!=current){

while (current.data != min) {

previous = current;

current = current.next;

}

previous.next=current.next;

current=previous.next;

}

}

/**

* 打印链表

* @param head

*/

public static void print(Node head){

String printStr="";

Node p=head.next;

while(null!=p){

printStr+=p.data+"-->";

p=p.next;

}

System.out.println(printStr.substring(0,printStr.length()-3));

}

/**

* 构造链表

* @param arr

* @return

*/

public static Node construct(int[] arr){

Node headNode=new Node();

Node p=headNode;

for(int a:arr){

Node dataNode=new Node();

dataNode.data=a;

dataNode.next=null;

p.next=dataNode;

p=dataNode;

}

return headNode;

}

/**

* 主方法

* @param args

*/

public static void main(String[] args) {

int [] arr=new int[]{6,3,5,2,9,7,6,4,3,2,6,6,9,6,3,4,6,5,2};

//构造

Node head=construct(arr) ;

//删除最小

delMin(head);

//打印结果

print(head);

}

}

以上是刚写出来的。按照你的要求构造了单链表,然后测试了删除最小节点的方法。如果不符合你的要求或者哪块代码不明白,可以追问或者私信我。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值