//核心代码模式
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
//这道题最坑的地方是,head不是头结点,head是第一个节点;
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null){
//System.out.println("单链表为空");
return head;
}
if(head.next == null){
return head;
}
ListNode reversehead = new ListNode();
reversehead.next = null;
ListNode cur = head;
ListNode next = null;
while(cur != null){
next = cur.next;
cur.next = reversehead.next;
reversehead.next = cur;
cur = next;
}
//return reversehead;
head = reversehead.next;
return head;
}
}