My Java solution


  • 0
    Q
    1. First find front node (m)
    2. Find tail node (n)
    3. Iterate forward from front node (n-m times), prepending each node to tail and redefiing tail
    4. Attach new tail to front node.
    
     public ListNode reverseBetween(ListNode head, int m, int n) {
        		ListNode dummy = new ListNode(-1);
                dummy.next = head;
                ListNode front = dummy;
                for (int i=1;i<m;i++){
                	front = front.next;
                }
                ListNode tail = front;
                for (int i=m-1;i<=n;i++){
                	tail = tail.next;
                }
                ListNode stop = tail;
                ListNode next = front.next;
                while(next!=stop){
                	ListNode temp = next.next;
                	next.next = tail;
                	tail = next;
                	next = temp;
                }
                front.next=tail;
                return dummy.next;
            }

Log in to reply
 

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