public class SolveQ7 {
public static void main(String[] args) {
GeeListNode node = new GeeListNode(1);
node.add(2);
node.add(3);
node.add(4);
node.add(5);
GeeListNode.print(node);
System.err.println("=========================");
GeeListNode.print(reverseList(node));
}
public static GeeListNode rollListNode(GeeListNode head){
GeeListNode pre = head;
GeeListNode current = head.next;
while(current != null){
GeeListNode next = current.next;
current.next = pre;
pre = current;
current = next;
}
head.next = null;
return pre;
}
public static GeeListNode reverseList(GeeListNode head) {
if(head==null||head.next==null)
return head;
GeeListNode node = reverseList(head.next);
head.next.next = head;
head.next = null;
return node;
}
}
class GeeListNode {
public Object val;
public GeeListNode next;
public GeeListNode(Object val) {
this.val = val;
}
public boolean add(Object val){
GeeListNode node = new GeeListNode(val);
if(this.next == null){
this.next = node;
}else{
this.next.add(val);
}
return true;
}
public void print(){
System.err.println(this.val +"\t");
if(this.next != null){
this.next.print();
}
}
public static void print(GeeListNode node){
System.err.println(node.val +"\t");
if(node.next != null){
node.next.print(node.next);
}
}
}
|