《剑指Offer》24. 反转链表

题目链接

牛客网

题目描述

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

解题思路

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;
    }
}
发布了206 篇原创文章 · 获赞 32 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_38611497/article/details/104148400