zoukankan      html  css  js  c++  java
  • 【LeetCode】Reverse Linked List II

    Reverse a linked list from position m to n. Do it in-place and in one-pass.

    For example:
    Given 1->2->3->4->5->NULLm = 2 and n = 4,

    return 1->4->3->2->5->NULL.

    Note:
    Given mn satisfy the following condition:
    1 ≤ m ≤ n ≤ length of list.

    public class Solution {
        public ListNode reverseBetween(ListNode head, int m, int n) {
            if(m==n||m>n)
                return head;
            int len = n-m;
            ListNode root = head;
            m--;
            while(m!=0&&root!=null){
                root = root.next;
                m--;
            }
            ListNode cur = root;
            while(len!=0&&len>0){
                ListNode temp = cur;
                int i=len;
                while(i!=0){
                    temp=temp.next;
                    i--;
                }
                    
                int tempv = cur.val;
                cur.val=temp.val;
                temp.val=tempv;
                len-=2;
                cur=cur.next;
            }
            
            return head;
            
        }
    }
  • 相关阅读:
    列表方块与格式与布局
    框架与样式表的基本概念
    表单
    内容容器,常用标签
    CSS3
    表单验证
    练习题
    document对象
    windows对象
    函数
  • 原文地址:https://www.cnblogs.com/yixianyixian/p/3721805.html
Copyright © 2011-2022 走看看