输入一个链表,反转链表后,输出新链表的表头。

别无他法 老老实实用上一堆指针

(或者直接记录值,更换值,很无聊,略)

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null||head.next==null) return head;
        ListNode prev=null;
        ListNode curr=head;
        ListNode next=head.next;
        while(curr.next!=null){
            curr.next=prev;
            prev=curr;
            curr=next;
            next=next.next;
        }
        curr.next=prev;
        return curr;
    }
}