栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

荷兰国旗问题(小于的放左边 大于的放右边 等于的放中间) - java链表题练习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

荷兰国旗问题(小于的放左边 大于的放右边 等于的放中间) - java链表题练习

给定一个单链表的头节点head,节点的值类型是整形,再给定一个整数pivot。实现一个调整链表的函数,将链表调整为左部分都是值小于pivot的节点,中间部分都是值等于pivot的节点,右部分都是值大于pivoit的节点

public Node method(Node head , int pivot){
	Node lH = null;
	Node lT = null;
	Node mH = null;
	Node mT = null;
	Node rH = null;
	Node rT = null;
	Node next = null;
	while(head != null){
		next = head.next;
		head.next = null;
		if(head.value < pivot){
			if(lH == null){
				lH = head;
				lT = head;	
			}else{
				lT.next = head;
				lT = head; 
			}
		}else if(head.value == pivot){
			if(mH == null){
				mH = head;
				mT = head;	
			}else{
				mT.next = head;
				mT = head; 
			}
		}else if(head.value > pivot){
			if(rH == null){
				rH = head;
				rT = head;	
			}else{
				rT.next = head;
				rT = head; 
			}
		}
		head = next;
	}
	if(lH != null){
		lT.next = mH;
		mT = mT == null ? lT : mT;
	}
	if(mH != null){
		mT.next = rH; 
	}
	return lH != null ? lH : (mH != null ? mH : rH); 
}	

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732643.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号