題目描述
輸入一個鏈表,反轉鏈表後,輸出新鏈表的表頭。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode newhead=null,p;
while(head!=null){
p=head;
head=head.next;
p.next=newhead;
newhead=p;
}
return newhead;
}
}