zoukankan      html  css  js  c++  java
  • [LeetCode] Reverse Linked List II @ Python [提供自创的示意图 Figure illustration]

    原题地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/

    题意:

    Reverse a linked list from position m to n. Do it in-place and in one-pass.

    For example:
    Given 1->2->3->4->5->NULLm = 2 and n = 4,

    return 1->4->3->2->5->NULL.

    Note:
    Given mn satisfy the following condition:
    1 ≤ m ≤ n ≤ length of list.

    解题思路:翻转链表的题目。请用积木化思维(Building block): 

    这里必须的积木:链表翻转操作:

    curr.next, prev, curr = prev, curr, curr.next

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        # @param head, a ListNode
        # @param m, an integer
        # @param n, an integer
        # @return a ListNode
        def reverseBetween(self, head, m, n):
            dummy, diff = ListNode(0), n - m
            dummy.next = head
            prev, curr = dummy, dummy.next
            while m > 1:
                prev, curr = curr, curr.next
                m -= 1
            last_unswapped, first_swapped = prev, curr
            while curr and diff >= 0:
                curr.next, prev, curr = prev, curr, curr.next
                diff -= 1
            last_unswapped.next, first_swapped.next = prev, curr
            return dummy.next
            # Here is an exmple
            # 1 --> 2 --> 3 --> 4 --> 5
            #       
            # Before first while-loop
            #      0 --> 1 --> 2 --> 3 --> 4 --> 5
            #      prev  curr
            # After first while-loop 
            #      0 --> 1 --> 2 --> 3 --> 4 --> 5 
            #            prev curr 
            #  last_unswapped, first_swapped = prev, curr
            # The following is the details for 2nd while-loop
            # diff=2 #   1 <-- 2     3 --> 4 --> 5
            #                 prev  curr
            # diff=1 #   1 <-- 2 <-- 3     4 --> 5
            #                       prev  curr
            # diff=0 #   1 <-- 2 <-- 3 <-- 4     5
            #                             prev  curr
            # last_unswapped.next = prev
            # first_swapped.next  = curr
            # After impletenting the above two lines, we get:
            #         first_swapped.next = curr
            #          __________________  
            #          ^                 |      
            #          |                 V 
            # 1        2 <-- 3 <-- 4     5
            # |                    ^
            # V ___________________| 
            # last_unswapped.next=prev    
            # Reverse partial Linked List
            # Refer Figure1: https://images0.cnblogs.com/i/546654/201404/072244468407048.jpg
            # Refer: http://www.cnblogs.com/4everlove/p/3651002.html
            # Refer: http://stackoverflow.com/questions/21529359/reversing-a-linked-list-in-python
  • 相关阅读:
    如何实现1080P延迟低于500ms的实时超清直播传输技术
    直播体验深度优化方案——连麦互动直播
    前向纠错码(FEC)的RTP荷载格式
    C++通过COM接口操作PPT
    neo4j的搭建和实例使用
    【neo4j】neo4j Desktop1.1.9,windows 安装
    Neo4j删除节点和关系、彻底删除节点标签名
    知识图谱赵军学习笔记(五)--实体消歧
    实体消歧简介
    svn服务器时间与本地时间不同步解决
  • 原文地址:https://www.cnblogs.com/asrman/p/3971933.html
Copyright © 2011-2022 走看看