剑指offer 专项突破版 24、反转链表

阅读 40

2022-03-13

题目链接

思路:一次遍历即可

很简单的题目,唯一需要注意的就是最初把pre设置成null

public class Solution {
    public ListNode reverseList(ListNode head) {
        if (null == head)
            return null;

        ListNode pre = null, now = head;

        while (null != now) {
            ListNode next = now.next;
            now.next = pre;
            pre = now;
            now = next;
        }
        return pre;
    }
}

精彩评论(0)

0 0 举报