Java与算法(7)

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);
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值