package Algorithm;
class Node{
int val;
Node next;
public Node(int val){
this.val=val;
}
}
public class ReverseList {
private static Node creatList(){
Node head = new Node(1);
Node p = head;
for (int i = 2; i <= 5;i++){
Node temp = new Node(i);
p.next = temp;
p = temp;
}
return head;
}
private static Node reverseList(Node head){
if(head==null){
return null;
}
Node newHead = head;
Node p = head.next;
while (p!=null){
head.next = p.next;
p.next = newHead;
newHead = p;
p= head.next;
}
return newHead;
}
public static void main(String[] args) {
Node head = creatList();
Node temp = head;
while (head != null){
System.out.print(head.val+" ");
head=head.next;
}
System.out.println();
Node newHead = reverseList(temp);
while (newHead != null){
System.out.print(newHead.val+" ");
newHead=newHead.next;
}
System.out.println();
}
}