leetcode || 92、Reverse Linked List II

problem:

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.

Hide Tags

Linked List

题意:翻转单链表第m~n个范围的结点

thinking:

O(n)算法

code:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *reverseBetween(ListNode *head, int m, int n) {
        if (head == NULL)
            return NULL;

        ListNode *q = NULL;
        ListNode *p = head;
        for(int i = 0; i < m - 1; i++)
        {
            q = p;
            p = p->next;
        }

        ListNode *end = p;
        ListNode *pPre = p;
        p = p->next;
        for(int i = m + 1; i <= n; i++)
        {
            ListNode *pNext = p->next;

            p->next = pPre;
            pPre = p;
            p = pNext;
        }

        end->next = p;
        if (q)
            q->next = pPre;
        else
            head = pPre;

        return head;
    }
};
时间: 2024-11-06 14:55:09

leetcode || 92、Reverse Linked List II的相关文章

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. 相比翻转整个链表稍微麻烦了那么一点,不过只要考虑好翻转的具体实现,问题都一样.AC代码如下: ListNode* reverse(

LeetCode OJ: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 ≤ lengt

92:Reverse Linked List II翻转链表【链表】

题目连接:click~ /*题意:将链表中第m到n个结点翻转 */ /** *思路:为更好处理表头和第m个结点,引入root结点,同时记录 * 第m-1个结点.从第m个结点开始遍历至第n个结点,将已经 * 遍历过的结点插入在第m-1个结点后,并保证第m个结点的next * 指向遍历结点的next,以避免链表断裂 */ class Solution { public: ListNode *reverseBetween(ListNode *head, int m, int n) { ListNode

leetcode -day30 Reverse Linked List II

1.  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 follow

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

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

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