题解 | #反转链表#

反转链表

http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here

        # double cursor

        if pHead == None or pHead.next == None:
            return pHead

        dummy_head = ListNode(0)
        dummy_head.next = pHead

        cur_1 = dummy_head
        cur_2 = pHead

        while cur_2:
            temp = cur_2.next
            cur_2.next = cur_1
            cur_1 = cur_2
            cur_2 = temp

        dummy_head.next.next = None 

        return cur_1 

利用双指针解决链表问题,时间复杂度为o(N),空间复杂度为o(1)

全部评论

相关推荐

10-20 11:11
辽宁大学 营销
点赞 评论 收藏
分享
11-07 15:54
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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