Reverse Linked List II

来源:互联网 发布:淘宝金冠女装店铺大全 编辑:程序博客网 时间:2024/06/07 02:08


/** * 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 (head == null) {            return null;        }        ListNode dummy = new ListNode(-1);        dummy.next = head;        head = dummy;        int cnt = 1;        while (cnt < m) {            head = head.next;            cnt++;        }        ListNode ptr = head.next;        while (cnt < n) {            ListNode temp = ptr.next;            ptr.next = temp.next;            temp.next = head.next;            head.next = temp;            cnt++;        }        return dummy.next;    }}


0 0
原创粉丝点击