链表操作 —— 206_反转链表

6. 206_反转链表
/*
反转一个单链表。
*/

class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode pre = null;
        ListNode cur = head;
        ListNode to = head.next;
        while(cur != null){
            cur.next = pre;
            pre = cur;
            cur = to;
            if(to != null) to = to.next;
        }
        return pre;
    }
}
原文地址:https://www.cnblogs.com/s841844054/p/13736304.html