标签:int node lin tno ble color 官方 first ebe
反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。
说明:
1 ≤ m ≤ n ≤ 链表长度。
示例:
输入: 1->2->3->4->5->NULL, m = 2, n = 4
输出: 1->4->3->2->5->NULL
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-linked-list-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
1 class Solution { 2 public ListNode reverseBetween(ListNode head, int m, int n) { 3 if (m == n) return head; 4 ListNode first = new ListNode(-1); 5 first.next = head; 6 7 ListNode last = first; 8 ListNode p = first, q = null, t = null, b = first; 9 for (int i = 0; i < m-1; i++) { 10 b = b.next; 11 } 12 p = b.next; 13 for (int i = 0; i <= n; i++) { 14 last = last.next; 15 } 16 q = p.next; 17 p.next = last; 18 for (; q != last; p = q, q = t) { 19 t = q.next; 20 q.next = p; 21 } 22 b.next = p; 23 return first.next; 24 } 25 }
标签:int node lin tno ble color 官方 first ebe
原文地址:https://www.cnblogs.com/yfs123456/p/11600217.html