zoukankan      html  css  js  c++  java
  • leetcode 92. 反转链表 II

    反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。

    说明:
    1 ≤ m ≤ n ≤ 链表长度。

    示例:

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


    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/reverse-linked-list-ii
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

     1 class Solution {
     2     public ListNode reverseBetween(ListNode head, int m, int n) {
     3         if (m == n) return head;
     4         ListNode first = new ListNode(-1);
     5         first.next = head;
     6 
     7         ListNode last = first;
     8         ListNode p = first, q = null, t = null, b = first;
     9         for (int i = 0; i < m-1; i++) {
    10             b = b.next;
    11         }
    12         p = b.next;
    13         for (int i = 0; i <= n; i++) {
    14             last = last.next;
    15         }
    16         q = p.next;
    17         p.next = last;
    18         for (; q != last; p = q, q = t) {
    19             t = q.next;
    20             q.next = p;
    21         }
    22         b.next = p;
    23         return first.next;
    24     }
    25 }
  • 相关阅读:
    selenium
    python第三方模块的安装
    程序员学习网站
    python 数据较大 性能分析
    linux ~/ 和 /
    VMWare虚拟机 window文件传递
    vi命令
    os.system
    win10系统进入BIOS
    pyinstaller将python脚本生成exe
  • 原文地址:https://www.cnblogs.com/yfs123456/p/11600217.html
Copyright © 2011-2022 走看看