上班摸鱼刷算法-Java-hot100-[206]反转链表

发布时间 2023-07-20 17:23:40作者: Hi,Bro
class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode preNode = null;
        ListNode nextNode = head;
        while (head != null) {
            nextNode = head.next;
            head.next = preNode;
            preNode = head;
            head = nextNode;
        }
        return preNode;
    }