单链表

单链表,遍历一次,找到中间节点。(这个主要要考虑到单链表元素个数是奇/偶数的情况,这个是关键的,我当时就是差点没考虑到这个)。


package Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListFindHalf {

private static Integer findHalf(Iterator it, Iterator itt) {
// Iterator itt=it;
// while (itt.hasNext()) {
// System.out.println("ss=" + itt.next());
// }
// while (it.hasNext()) {
// System.out.println("tt=" + it.next());
// }
// while (it.hasNext()){
// it.next();
// if (it.hasNext()){
// it.next();
// itt.next();
// } else{
// // System.out.println("sd="+ itt.next());
// return (Integer) itt.next();
// }
// }
// return 8888;
do {
if (it.hasNext()) {
it.next();
}
if (it.hasNext()) {
it.next();
if (it.hasNext()) {
itt.next();
}
} else {
return (Integer) itt.next();
}
} while (it.hasNext());
return 8888;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 8;
List list = new ArrayList();
for (int i = 0; i <= n; i++) {
list.add(i);
}
System.out.println("halfPoint value="
+ findHalf(list.iterator(), list.iterator()).intValue());
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值