• 链表指定区间反转


    题目:反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。
    说明:
    1 ≤ m ≤ n ≤ 链表长度。

    输入:1->2->3->4->5->NULL, m = 2, n = 4

    输出:1->4->3->2->5->NULL

    头插法

    Java实现

    1. public static ListNode reverseBetween2(ListNode head, int left, int right) {
    2. // 设置 dummyNode 是这一类问题的一般做法
    3. ListNode dummyNode = new ListNode(-1);
    4. dummyNode.next = head;
    5. ListNode pre = dummyNode;
    6. for (int i = 0; i < left - 1; i++) {
    7. pre = pre.next;
    8. }
    9. ListNode cur = pre.next;
    10. ListNode next;
    11. for (int i = 0; i < right - left; i++) {
    12. next = cur.next;
    13. cur.next = next.next;
    14. next.next = pre.next; //注意此处,为啥不能用cur
    15. pre.next = next;
    16. }
    17. return dummyNode.next;
    18. }

    python实现

    1. def reverseBetween2(self, head, left, right):
    2. # 设置 dummyNode 是这一类问题的一般做法
    3. dummy_node = ListNode(-1)
    4. dummy_node.next = head
    5. pre = dummy_node
    6. for _ in range(left - 1):
    7. pre = pre.next
    8. cur = pre.next
    9. for _ in range(right - left):
    10. next = cur.next
    11. cur.next = next.next
    12. next.next = pre.next
    13. pre.next = next
    14. return dummy_node.next

    穿针引线法

    Java实现

    1. public static ListNode reverseListNode(ListNode head, int left, int right) {
    2. //特殊情况考虑
    3. if(head == null || head.next == null || left == right){
    4. return head;
    5. }
    6. //建立虚拟节点
    7. ListNode dummyNode = new ListNode(-1);
    8. dummyNode.next = head;
    9. ListNode temp = dummyNode;
    10. int count =0;
    11. //找到left前一个节点
    12. for(;count < left-1;count++){
    13. temp = temp.next;
    14. }
    15. //反转链表开始的位置
    16. ListNode leftNode = temp.next;
    17. //记录反转后链表的头节点
    18. ListNode rightNode = null;
    19. ListNode next = null;
    20. //开始反转链表
    21. for(;count
    22. next = leftNode.next;
    23. leftNode.next = rightNode;
    24. rightNode = leftNode;
    25. leftNode = next;
    26. }
    27. ListNode curRight = rightNode;
    28. //遍历到链表的尾部
    29. while (curRight.next != null){
    30. curRight = curRight.next;
    31. }
    32. //让反转区间和区间后面的位置相连接
    33. curRight.next = leftNode;
    34. //让区间前面的和反转区间相连接
    35. temp.next = rightNode;
    36. return dummyNode.next;
    37. }

    python实现

    1. def reverseBetween(self, head, left, right):
    2. def reverse_linked_list(head):
    3. # 也可以使用递归反转一个链表
    4. pre = None
    5. cur = head
    6. while cur:
    7. next = cur.next
    8. cur.next = pre
    9. pre = cur
    10. cur = next
    11. # 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论
    12. dummy_node = ListNode(-1)
    13. dummy_node.next = head
    14. pre = dummy_node
    15. # 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点
    16. # 建议写在 for 循环里,语义清晰
    17. for _ in range(left - 1):
    18. pre = pre.next
    19. # 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点
    20. right_node = pre
    21. for _ in range(right - left + 1):
    22. right_node = right_node.next
    23. # 第 3 步:切断出一个子链表(截取链表)
    24. left_node = pre.next
    25. curr = right_node.next
    26. # 注意:切断链接
    27. pre.next = None
    28. right_node.next = None
    29. # 第 4 步:反转链表的子区间
    30. reverse_linked_list(left_node)
    31. # 第 5 步:接回到原来的链表中
    32. pre.next = right_node
    33. left_node.next = curr
    34. return dummy_node.next

  • 相关阅读:
    mysql数据库中的插入数据insert,中文字符集配置
    电源方案对比
    【WPF】填坑 - WindowChrome 自定义窗口完美实现
    力扣每日一题 - 【单词搜索】
    后端传递数据给前端做导出Excel的vo类
    opencv语法Mat类型总结
    基于PLC的工业晾晒架系统
    【MybatisPlus】BaseMapper详解,举例说明
    springboot+视频网站 毕业设计-附源码240925
    自适应对话式团队构建,提升语言模型代理的复杂任务解决能力
  • 原文地址:https://blog.csdn.net/programer666bird/article/details/132737497