题目链接

牛客网

题目描述

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

解题思路

public class Solution {
   
    public ListNode ReverseList(ListNode head) {
   
        if (head==null || head.next==null) return head;
        ListNode pre = null, cur = head;
        while (cur!=null) {
   
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}