Memory Exceeded Limit


  • 0
    H
    
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode reverseBetween(ListNode head, int m, int n) {
            //if both are same return the list as in
            if(m==n)
            return head;
            
            //iterate till the start node
            int count=1;
            ListNode mainIter = head;
            while(count++<m){
                mainIter = mainIter.next;
            }
            //make the swap
            ListNode iter = mainIter.next;
            while(count++<=n){
                ListNode nextNode = iter.next;
                iter.next = mainIter;
                mainIter = iter;
                iter = nextNode;
            }
            return head;
            
        }
    }
    

    Not sure what is wrong here. Can someone help here?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.