Java与算法(7)
1.对单链表进行选择排序
public class SelectionSortded {
public static class Node {
int data;
Node next;
public Node (int data) {
this .data = data;
}
}
public void printLink (Node head) {
while (head!=null ) {
System.out.println(head.data+" " );
head=head.next;
}
System.out.println();
}
public Node getSmallPre (Node head) {
Node smallPre = null ;
Node small = head;
Node pre = head;
Node cur = head.next;
while (cur!=null ) {
if (cur.data<small.data) {
smallPre = pre;
small = cur;
}
pre = cur;
cur = cur.next;
}
return smallPre;
}
public Node sort (Node head) {
Node small = null ;
Node smallPre = null ;
Node tail = null ;
Node cur = head;
while (cur!=null ) {
small = cur;
smallPre=getSmallPre(cur);
if (smallPre!=null ) {
small = smallPre.next;
smallPre.next = small.next;
}
cur = cur==small?cur.next:cur;
if (tail==null ) {
head = small;
} else {
tail.next = small;
}
tail = small;
}
return head;
}
public static void main (String[] args) {
Node node1 = new Node(1 );
node1.next = new Node(9 );
node1.next.next = new Node(3 );
node1.next.next.next = new Node(15 );
node1.next.next.next.next = new Node(4 );
node1.next.next.next.next.next = new Node(2 );
node1.next.next.next.next.next.next = new Node(0 );
SelectionSortded selectionSortded = new SelectionSortded();
selectionSortded.printLink(node1);
System.out.println("-------" );
selectionSortded.sort(node1);
selectionSortded.printLink(node1);
}
}
2.删除单链表指定数值的节点
public class DeleteNum {
public static class Node {
int data;
Node next;
public Node (int data) {
this .data = data;
}
}
public void printLink (Node head) {
while (head!=null ) {
System.out.println(head.data+" " );
head=head.next;
}
System.out.println();
}
public Node delete (Node head,int num) {
while (head!=null ) {
if (head.data!=num) {
break ;
}
head=head.next;
}
Node pre = head;
Node cur = head;
while (cur!=null ) {
if (cur.data==num) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
return head;
}
public static void main (String[] args) {
Node node1 = new Node(1 );
node1.next = new Node(2 );
node1.next.next = new Node(3 );
node1.next.next.next = new Node(3 );
node1.next.next.next.next = new Node(4 );
node1.next.next.next.next.next = new Node(5 );
node1.next.next.next.next.next.next = new Node(5 );
DeleteNum deleteNum = new DeleteNum();
deleteNum.printLink(node1);
System.out.println("-------" );
Node node = deleteNum.delete(node1, 3 );
deleteNum.printLink(node);
}
}
3.删除单链表中重复出现的节点
public class DeleteSameOne {
public static class Node {
int data;
Node next;
public Node (int data) {
this .data = data;
}
}
public void printLink (Node head) {
while (head!=null ) {
System.out.println(head.data+" " );
head=head.next;
}
System.out.println();
}
public void delete (Node head) {
HashSet<Integer> hashSet = new HashSet<>();
Node pre = head;
Node cur = head.next;
hashSet.add(head.data);
while (cur!=null ) {
if (hashSet.contains(cur.data)) {
pre.next = cur.next;
} else {
hashSet.add(cur.data);
pre = cur;
}
cur=cur.next;
}
}
public static void main (String[] args) {
Node node1 = new Node(1 );
node1.next = new Node(2 );
node1.next.next = new Node(3 );
node1.next.next.next = new Node(3 );
node1.next.next.next.next = new Node(4 );
node1.next.next.next.next.next = new Node(5 );
node1.next.next.next.next.next.next = new Node(5 );
DeleteSameOne deleteSameOne = new DeleteSameOne();
deleteSameOne.printLink(node1);
System.out.println("--------" );
deleteSameOne.delete(node1);
deleteSameOne.printLink(node1);
}
}