zoukankan      html  css  js  c++  java
  • leetcode 206. Reverse Linked List

    Reverse a singly linked list.

    click to show more hints.

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def reverseList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if not head: return None
            pre_node = head
            next_node = head.next
            pre_node.next = None
            while next_node:
                tmp = next_node.next
                next_node.next = pre_node
                pre_node = next_node
                next_node = tmp
            return pre_node        

    精简下代码:

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def reverseList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if not head: return None
            pre_node = None
            cur_node = head
            while cur_node:
                tmp = cur_node.next
                cur_node.next = pre_node
                pre_node = cur_node
                cur_node = tmp
            return pre_node        

    递归解:

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def reverseList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            def reverse_node(pre_node, cur_node):
                if not cur_node: return pre_node
                next_node = cur_node.next
                cur_node.next = pre_node
                return reverse_node(cur_node, next_node)
                 
            if not head: return None
            new_head = reverse_node(head, head.next)
            head.next = None
            return new_head                                  

    dfs

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def reverseList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if not head or not head.next: return head
            node = self.reverseList(head.next)
            head.next.next = head
            head.next = None
            return node                

    记得程序员面试金典里专门提过!

  • 相关阅读:
    python把汉字转换成拼音实现程序
    C#第三方zip解压压缩工具,带事例源码
    实现在线压缩文件的实现程序代码
    如何处理JSON中的特殊字符
    《汉字简体、繁体相互转换》 查看源代码
    中文字符转拼音源码
    upper_bound()函数使用方法
    CodeFroces New Assignment 二分图匹配
    选拔赛 hash 字符串匹配 哈希算法(白书p374)
    lower_bound()函数使用
  • 原文地址:https://www.cnblogs.com/bonelee/p/8687153.html
Copyright © 2011-2022 走看看