题一:取出LinkedList的中间数
LinkedList底层是链表Node(E).
常用函数有size(), add(), clear(), contain(), get(), getFirst(), getLast().
用size = list.size(), list.get(size/2)即取出中间数的element.
题二:链表反转
对于一个链表,实现反转。比如: a -> b -> c ->d 反过来就是 d -> c -> b -> a 。
java源码如下:
class Node{
char value;
Node next;
}
//非递归实现
public Node reverse(Node current) {
Node previous = null;
Node next = null;
while (current != null) {
//存储下一节点
next = current.next;
current.next = previous; //反转
//更新遍历节点
previous = current;
current = next;
}
return current;
}