【LeetCode 92.】 反转链表 II

1.题目

虽然本题很好拆解,但是实现起来还是有一些难度的。

2. 分析

  • 尽可能抽象问题,然后简化代码

我在写本题的时候,遇到了下面这两个问题:

  • 没有把[left,right] 这个区间的链表给断开,所以导致反转起来非常麻烦。所以在找到[left, right] 区间后,要将这个链表前后断开会比较方便操作。
  • 正是因为问题1,导致我在反转链表的时候,使用了下面这版代码:
python 复制代码
# 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) :
        # 如果区间为1,不用反转
        if left == right:
            return head
        
        cnt = 1
        head_bak = head
        while(cnt < left):
            head_bak = head_bak.next
            cnt+=1
        new_left = head_bak

        head_bak = head
        cnt = 1
        while(cnt < right):
            head_bak = head_bak.next
            cnt+=1
        new_right = head_bak

        print(new_left.val, new_right.val)
        reversed_head = new_right
        reversed_tail = new_left

        split_head = head
        split_tail = new_right.next
        while(split_head.next != new_left):
            split_head = split_head.next

        # 开始反转
        pre = None
        print("hhh",new_right.next.val)
        cnt = 0 # 反转节点的个数
        # while(cnt < right-left+2):        
        while(new_left != new_right.next):
            print(id(new_right.next))
            tmp = new_left.next
            new_left.next = pre
            pre = new_left
            new_left = tmp
            cnt+=1
            # print(new_left.val , new_left == new_right.next, id(new_left), id(new_right.next))

        split_head.next = reversed_head
        reversed_tail.next = split_tail
        
        if reversed_tail == head:
            return reversed_head
        return head

head1 = ListNode(1)
head2 = ListNode(2)
head3 = ListNode(3)
head4 = ListNode(4)
head5 = ListNode(5)
head1.next = head2
head2.next = head3
head3.next = head4
head4.next = head5
head5.next = None
start = head1
while(start):
    print(id(start))
    start = start.next
s = Solution()
s.reverseBetween(head1, 2, 4)

这份代码有一个隐蔽的bug:

在 41 ~ 47 行之间。原因是 while 循环的过程中会把 new_right.next 的值给改掉(也就是44行的代码),因为new_right 指的是right那个地方的节点,这个节点new_left 在遍历的过程中也会访问。

这份代码最大的问题就是没有意识到这个new_right.next 值在while时候变化了。

3.代码

下面这份代码虽然可以过掉样例,但是代码很丑。

python 复制代码
# 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) :
        # 如果区间为1,不用反转
        if left == right:
            return head
        
        cnt = 1
        head_bak = head
        while(cnt < left):
            head_bak = head_bak.next
            cnt+=1
        new_left = head_bak

        head_bak = head
        cnt = 1
        while(cnt < right):
            head_bak = head_bak.next
            cnt+=1
        new_right = head_bak

        print(new_left.val, new_right.val)
        reversed_head = new_right
        reversed_tail = new_left

        split_head = None
        split_tail = new_right.next
        cnt = 1
        hh_head = head
        while(cnt < left):
            split_head = hh_head
            hh_head = hh_head.next            
            cnt += 1

        # 开始反转
        pre = None
        # print("hhh",new_right.next.val)
        cnt = 0 # 反转节点的个数
        while(cnt < right-left+1):
            # print(id(new_right.next))
            tmp = new_left.next
            new_left.next = pre
            pre = new_left
            new_left = tmp
            cnt+=1
            # print(new_left.val , new_left == new_right.next, id(new_left), id(new_right.next))
        if split_head:
            split_head.next = reversed_head
        reversed_tail.next = split_tail
        
        if reversed_tail == head:
            return reversed_head
        return head
相关推荐
努力学算法的蒟蒻13 分钟前
day88(2.17)——leetcode面试经典150
算法·leetcode·面试
@––––––19 分钟前
力扣hot100—系列6-栈
linux·python·leetcode
Anastasiozzzz31 分钟前
LeetCode 287 寻找重复数字
算法·leetcode·职场和发展
im_AMBER31 分钟前
Leetcode 123 二叉树的层平均值 | 二叉树的右视图 | 二叉树的层序遍历
数据结构·学习·算法·leetcode·二叉树
We་ct31 分钟前
LeetCode 100. 相同的树:两种解法(递归+迭代)详解
前端·算法·leetcode·链表·typescript
样例过了就是过了38 分钟前
LeetCode热题100 轮转数组
数据结构·算法·leetcode
ShineWinsu39 分钟前
对于stack和queue经典算法题目:155. 最小栈、JZ31 栈的压入、弹出序列和102. 二叉树的层序遍历的解析
数据结构·c++·算法·面试·力扣·笔试·牛客网
能源系统预测和优化研究42 分钟前
【原创改进代码】考虑电动汽车移动储能特性的多区域电网功率波动平抑优化调控
大数据·算法·能源
_F_y42 分钟前
两个数组的动态规划
算法·动态规划
每天要多喝水43 分钟前
动态规划Day32:最长公共子序列
算法·动态规划