public ListNode reverseList(ListNode head) {
if(head==null||head.next==null)return head;
if(head.next.next==null){
ListNode temp;
temp = head.next;
head.next.next = head;
head.next = null;
return temp;
}
ListNode first = head,second = head.next,third = head.next.next;
head.next = null;
while(third!=null){
second.next = first;
first = second;
second = third;
third = third.next;
}
second.next = first;
return second;
}
本题较为简单,要注意的是原头指针所指向的next要释放为空。