微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

反转子列表

如何解决反转子列表

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

给定一个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 时会发生这种情况,这很好,我理解那部分,但我以为我又重新连接了我的列表。

解决方法

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 to head else set previous to connection.next

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

完整代码:

def reverse_sub_list(head,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

,

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。