输入一个链表,反转链表后,输出链表的所有元素。
运行时间:38ms 占用内存:528k
leetcode原题
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
<span style="white-space:pre"> </span>ListNode newhead=null;
ListNode pre=new ListNode(0);
while(head!=null){
pre=head.next;
head.next=newhead;
newhead=head;
head=pre;
}
/*while(newhead!=null){
System.out.print(newhead.val);
newhead=newhead.next;
}*/
return newhead;
}
}