Share my java solution.


  • 0
    H
    public class Solution {
        public ListNode reverseBetween(ListNode head, int m, int n) {
            if(head == null || head.next == null){
                return head;
            }   
            ListNode dummy = new ListNode(-1);
            dummy.next = head;
            ListNode nodem = dummy;
            ListNode noden = dummy;
            int count = 0;
            while(count < m - 1){
                count++;
                nodem = nodem.next;
            }
            ListNode cur = nodem.next;
            ListNode next = cur.next;
            while(next != null){
                if(count == n - 1){
                    break;
                }
                ListNode temp = next.next;
                next.next = nodem.next;
                nodem.next = next;
                cur.next = temp;
                next = temp;
                count++;
            }
            return dummy.next;
        }
    }

Log in to reply
 

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