链表操作 —— 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;
    }
}
posted @ 2020-09-26 19:23  SSunSShine  阅读(72)  评论(0编辑  收藏  举报