package offer;
public class {
static class ListNode{
int data;
ListNode next;
}
public ListNode ReverseList(ListNode head){
if(head == null)
return null;
ListNode preListNode = null;
ListNode nowListNode = head;
while(nowListNode != null){
ListNode nextListNode = nowListNode.next;
nowListNode.next = preListNode;
preListNode = nowListNode;
nowListNode = nextListNode;
}
return preListNode;
}
public static void main(String[] args){
ListNode head = new ListNode();
ListNode second = new ListNode();
ListNode third = new ListNode();
ListNode forth = new ListNode();
head.next = second;
second.next = third;
third.next = forth;
head.data = 1;
second.data = 2;
third.data = 3;
forth.data = 4;
t16 test = new t16();
ListNode result = test.ReverseList(head);
System.out.println(result.data);
}
}
近期评论