Is my solution in-place and one pass?


  • 1
    N
    public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n)
        {
            if (head == null || head.next == null)
            {
                return head;
            }
            int position = 1;
            ListNode node = head;
            ListNode previous = null;
            while (node != null && node.next != null)
            {
                position++;
    
                if (position > n)
                {
                    return head;
                }
    
                if (position > m)
                {
                    ListNode temp = node.next;
    
                    node.next = node.next.next;
    
                    if (previous != null)
                    {
                        temp.next = previous.next;
                        previous.next = temp;
                    }
                    else
                    {
                        temp.next = head;
                        head = temp;
                    }
                    
                }
                else
                {
                    // next node
                    previous = node;
                    node = node.next;
                }
    
            }
    
            return head;
        }
    

    }


Log in to reply
 

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