Reverse Linked List II leetcode

本题与之前单链表逆置不同的是,加入了范围判断。

依然沿用之前单链表逆置的方法,只需要再做好起始节点和末尾节点的判断

说起来容易,做起来复杂,特别是单链表,很容易把人搞晕,所以,在编程之前最后画图理清思路。

这次在头结点的处理上,不同于以往设置临时头结点的方法,使用了二级指针,这种方法写出来的代码可能比较少,但是不容易理解,对于追求效率的同学未尝不可一试

ListNode* reverseBetween(ListNode* head, int m, int n) {
    if (head == nullptr || head->next == nullptr)
        return head;
    int count = n - (m--) - 1;
    ListNode **pos = &head;
    while (m-- && (*pos)->next != nullptr)
        pos = &((*pos)->next);
    if ((*pos)->next == nullptr)
        return head;
    ListNode *currPos = (*pos)->next;
    ListNode *nextPos = (*pos)->next->next;
    ListNode *prevPos = (*pos);
    while (count-- > 0 && nextPos != nullptr)
    {
        // 改变currPos->next
        currPos->next = prevPos;

        // 依次更新prev、curr、next(向后移动)
        prevPos = currPos;
        currPos = nextPos;
        nextPos = nextPos->next;
    }
    if (count == -1) {
        currPos->next = prevPos; // 注意最后一步
        ListNode *temp = *pos;
        *pos = currPos;
        temp->next = nextPos;
    }
    return head;
}
时间: 2024-10-08 01:51:57

Reverse Linked List II leetcode的相关文章

Reverse Linked List II leetcode 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

Reverse Linked List II——LeetCode

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——Reverse Linked List II 选择链表中部分节点逆序(AC)

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:Reverse Linked List II 反转链表区间

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

leetcode - Reverse Linked List II

leetcode - 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 fol

LeetCode OJ - 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 ≤ l

[LeetCode][JavaScript]Reverse Linked List II

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 cond

【LeetCode】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 --- Reverse Linked List II(旋转链表的指定部分)

Reverse Linked List II(旋转链表的指定部分) Leetcode 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 sati