Simple Java Solution


  • 0

    ...

    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dumy = new ListNode(Integer.MIN_VALUE);
        dumy.next = head;
        
        ListNode begin = dumy;
        for (int i = 1; i < m; i++) {
            begin = begin.next;
        }
        
        ListNode cur = begin.next;
        for (int i = m; i < n; i++) {
            ListNode temp = cur.next.next;
            cur.next.next = begin.next;
            begin.next = cur.next;
            cur.next = temp;
        }
        
        return dumy.next;
    }
    

    ...


Log in to reply
 

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