zoukankan      html  css  js  c++  java
  • [LeetCode]92. Reverse Linked List II反转部分链表

    /*
        重点还是反转链表
        思路就是中间的反转,然后两头接上
         */
        public ListNode reverseBetween(ListNode head, int m, int n) {
            if (head==null||m>=n) return head;
            int count = 1;
            ListNode sta = head;
            //mid就是第一个接点的前节点
            ListNode mid = null;
            while (count<m)
            {
                mid = head;
                head = head.next;
                count++;
            }
            //中间的部分反转,此时的head是反转的开头
            ListNode pre = null;
            while (count<=n)
            {
                ListNode next = head.next;
                head.next = pre;
                pre = head;
                head = next;
                count++;
            }
            //将后边的接上
            ListNode end = pre;
            while (end.next!=null) end = end.next;
            end.next = head;
            //如果是从第一个开始,要单独考虑
            if (mid!=null) mid.next = pre;
            else return pre;
            return sta;
        }
  • 相关阅读:
    团队个人冲刺day08
    4.26 jQuery AJAX load() 方法
    4.23 jquery
    4.22 AJAX技术
    4.21 正则表达式
    4.20
    4.15 重写团队作业1
    4.12 重写团队作业1
    4.9 团队作业1
    4.7 团队作业1
  • 原文地址:https://www.cnblogs.com/stAr-1/p/8423551.html
Copyright © 2011-2022 走看看