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 ≤ length of list.

处理这个问题还是挺复杂的,需要考虑很多边界的测试用例。我总体的思路是先用循环标记m前一个节点和n后边一个节点,把n后边的节点首先作为当前逆转节点的pre,然后循环n-m次完成所选节点部分的逆序,然后将标记的m节点前一个节点指向逆序后部分的头节点即可。要考虑各种特殊情况,另外考虑即可。code如下:

class Solution {
public:
    ListNode *reverseBetween(ListNode *head, int m, int n) {
        if(head==NULL || m<0 || n<0)
            return head;
        if(head->next == NULL || m==n)
            return head;
        ListNode *head2=NULL,*pre,*cur,*temp=head;
        for(int i=0; i<n+1; i++)
        {
            if(i==m-2)
                head2=temp;
            else if(i==m-1)
                cur=temp;
            else if(i==n)
                pre=temp;
            if(temp!=NULL)
                temp = temp->next;
        }
        for(int i=m;i<n+1;i++)
        {
            temp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        if(m==1)
            return pre;
        head2->next = pre;
        return head;
    }
};

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC),布布扣,bubuko.com

时间: 2024-08-02 02:51:10

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)的相关文章

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 ≤ 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(旋转链表的指定部分)

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

[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 a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2->3->4->5->NULL, m = 2 and n = 4, return1->4->3->2->5->NULL. Note:  Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

LeetCode: Reverse Linked List II [092]

[题目] 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

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:Swap Nodes in Pairs 单链表相邻两节点逆置

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, on

leetcode_92题——Reverse Linked List II(链表操作)

Reverse Linked List II Total Accepted: 40420 Total Submissions: 154762My Submissions Question Solution 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, retur