展开全部
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);
}
}
以上是刚写出来的。按照你的要求构造了单链表,然后测试了删除最小节点的方法。如果不符合你的要求或者哪块代码不明白,可以追问或者私信我。