LeetCode-206.ReverseLinked List


Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Follow up:A linked list can be reversed either iteratively or recursively. Could you implement both?

public ListNode reverseList(ListNode head) {//链表 迭代 my
        ListNode nhead = null;
        ListNode curr = head;
        while(null!=curr){
            head=head.next;
            curr.next=nhead;
            nhead=curr;
            curr=head;
        }
        return nhead;
    }  

  

递归

public ListNode reverseList(ListNode head) {
    if (head == null || head.next == null) return head;
    ListNode p = reverseList(head.next);
    head.next.next = head;
    head.next = null;
    return p;
}

 

进阶题

两两交换链表中的结点 LeetCode24https://www.cnblogs.com/zhacai/p/10559271.html 

每 k 个节点一组翻转链表LeetCode25https://www.cnblogs.com/zhacai/p/10563446.html

优质内容筛选与推荐>>
1、strip,lstrip,rstrip,split(字符串处理)
2、分析并解决FLV格式文件上传服务器后不能播放
3、leetcode 之 Two Sum II - Input array is sorted c++
4、C++ STL 的实现
5、对io进行分流


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn