92. Reverse Linked List II - Medium

Reverse a linked list from position m to n. Do it in one-pass.

Note: 1 ≤ m ≤ n ≤ length of list.

Example:

Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL

只反转从m到n的部分链表

M1: iterative

首先找到prev的位置(开始反转位置的前一个节点),cur为prev的后一个节点。然后开始反转,循环n-m次,在循环里,prev的位置始终保持不变,先保存cur下一个节点的位置,即nextnode,cur指向nextnode的下一个节点,nextnode指向prev的下一个节点,prev指向nextnode。

e.g.

1->2->3->4->5->NULL    2->4, 3->2, 1->3 => 1->3->2->4
p   c    n

1->3->2->4->5->NULL    2->5, 4->3, 1->4 => 1->4->3->2->5
p         c    n

1->4->3->2->5->NULL
p      c    n

time: O(n), space: O(1)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        int len = n - m;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode prev = dummy, cur = dummy.next;
        while(m > 1) {          // find prev
            prev = prev.next;
            cur = cur.next;
            m--;
        }
        // return prev;
        while(len > 0) {
            ListNode nextnode = cur.next;
            cur.next = nextnode.next;
            nextnode.next = prev.next;
            prev.next = nextnode;
            len--;
        }
        return dummy.next;
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10122417.html

时间: 2024-11-05 18:39:24

92. Reverse Linked List II - Medium的相关文章

92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

LeetCode OJ 92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

92. Reverse Linked List II (List)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

【leetcode】92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

leetCode 92.Reverse Linked List II (反转链表II) 解题思路和方法

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

[leedcode 92] Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

[LeetCode] 92. Reverse Linked List II Java

题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ l

LeetCode 92. Reverse Linked List II

将链表分成三段,中间一段reverse后再链接. 链表中节点位置从1开始计数. 注意m可能为1,所以要加上if(last == start) head = end;    不然会丢失数据,因为reverse后head是reverse一段的最后一节点,只返回head的话head之前的节点全部丢失. 4ms 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *nex

【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List

The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three pointers to achieve this goal. 1) Pointer to last value 2) Pointer to cur p value 3) Pointer to next value Here, showing my code wishes can help u. #incl