《剑指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;
    }
}
全部评论

相关推荐

双尔:反手回一个很抱歉,经过慎重考虑,您与我的预期暂不匹配,感谢您的投递
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务