【问题标题】:Reverse a sub list反转子列表
【发布时间】:2021-10-02 16:31:10
【问题描述】:

我必须反转我的链表中的子列表,然后返回原始链表,但子列表反转。问题如下……

给定 LinkedList 的头部和两个位置“p”和“q”,将 LinkedList 从位置“p”反转为“q”。

例如

1 -> 2 -> 3 -> 4 -> 5 -> null, p = 2, q = 4 then,
1 -> 4 -> 3 -> 2 -> 5 -> null
from __future__ import print_function


class Node:
  def __init__(self, value, next=None):
    self.value = value
    self.next = next

  def print_list(self):
    temp = self
    while temp is not None:
      print(temp.value, end=" ")
      temp = temp.next
    print()


def reverse_sub_list(head, p, q):
  start, end = head, head
  previous = None

  while p > 1:
    previous = start
    start = start.next
    p -= 1
  
  while q > 0:
    end = end.next
    q -= 1

  last_node_of_sub_list = start
  sub_list = reverse(start, end)
  previous.next = sub_list
  last_node_of_sub_list.next = end
  return head

  '''
  first_list = head
  last_list = end

  while first_list is not None:
    first_list = first_list.next
  first_list = sub_list

  while sub_list is not None:
    sub_list = sub_list.next
  sub_list = last_list

  return head
  '''

def reverse(head, last_node):
  previous = None

  while head is not last_node:
    _next = head.next
    head.next = previous
    previous = head
    head = _next
  
  return previous


def main():
  head = Node(1)
  head.next = Node(2)
  head.next.next = Node(3)
  head.next.next.next = Node(4)
  head.next.next.next.next = Node(5)

  print("Nodes of original LinkedList are: ", end='')
  head.print_list()
  result = reverse_sub_list(head, 2, 4)
  print("Nodes of reversed LinkedList are: ", end='')
  result.print_list()


main()

在我的 while 循环中,我认为我将first_list 的最后一个节点连接到sub_list,并将sub_list 的最后一个节点连接到last_list

原来当我返回head时,head现在只有1 -> 2 -> null 当我反转 sub_list 时会发生这种情况,这很好,我理解那部分,但我认为我正在重新连接我的列表。

【问题讨论】:

  • start (pre_start) 之前保存节点。然后pre_start.next = sub_list。不知道最后两个时间和任务应该完成什么。
  • 好的,我知道我可以做到这一点并添加了该部分。我还添加了连接到列表最后一部分的部分,并且列表被分成三个部分部分。我仍然不知道为什么我的 while 循环不起作用。我认为这本质上是一样的。找到第一个列表中的最后一个节点并将其指向子列表的开头并找到子列表的最后一个节点并将其指向我现在看到的最后一个列表是行不通的,因为它正在走另一条路。但是,我不明白为什么第一个列表和子列表不附加
  • 其实,没关系。我现在明白了。我的第一个列表最后一个节点与我的子列表的第一个节点相同。通过从第一个列表的第一个节点执行 first_list.next,它可以正确连接第一个列表和子列表。

标签: python reverse singly-linked-list


【解决方案1】:
def reverse_sub_list(head, p, q):
  start, end = head, head
  previous = None

  while p > 1:
    previous = start
    start = start.next
    p -= 1
  

现在在使用q > 0 while 循环之前,您可以只存储上一个和当前的值,然后继续使用q > 0 while 循环。 而在这个循环中,反过来做,q递减

connection = previous
tail = current
while q > 0:
     currentNext = current.next
     current.next = previous
     previous = current
     current = currentNext
     q--

现在我们得到的只是连接是否为空。如果连接为空,则可以将previous设置为head,否则将previous设置为connection.next

if connection != None:
   connection.next = previous
else:
   head = previous
tail.next = current
return head

完整代码:

def reverse_sub_list(head, p, q):
  current = head
  previous = None

  while p > 1:
    previous = start
    current = current.next
    p -= 1

  connection = previous
  tail = current
  while q > 0:
     currentNext = current.next
     current.next = previous
     previous = current
     current = currentNext
     q--
  
  if connection != None:
    connection.next = previous
  else:
    head = previous
  tail.next = current
  return head

您可以查看此链接以获取更多解释和图:Click here

【讨论】:

    【解决方案2】:

    https://leetcode.com/problems/reverse-linked-list-ii/description/

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, val=0, next=None):
    #         self.val = val
    #         self.next = next
    class Solution:
        def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:
            if left == right:
                return head
            prev = start = prev_start = None
            node = head
    
            for count in range(1, right + 1):
                if count >= left:
                    if not start:
                        prev_start = prev
                        start = node
                    nxt = node.next
                    node.next = prev
                    prev = node
                    node = nxt
                else:
                    prev = node
                    node = node.next
    
            if prev_start:
                prev_start.next = prev
            if start:
                start.next = node
    
            return head if left > 1 else prev
    

    【讨论】:

      猜你喜欢
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 2012-08-20
      • 1970-01-01
      • 2021-05-21
      • 2020-12-05
      • 1970-01-01
      相关资源
      最近更新 更多