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->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

Subscribe to see which companies asked this question

解答:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseBetween(struct ListNode* head, int m, int n) {
    struct ListNode *pNode = head, *tmp_1, *tmp_2, *tmp_head = NULL, *tmp_tail;
    int count = 0;

    if(1 != m){
        while(NULL != pNode){
            count++;
            if(m - 1 == count){
                tmp_tail = tmp_1 = pNode->next;
                pNode->next = NULL;
                while(n > count){
                    tmp_2 = tmp_1->next;
                    tmp_1->next = pNode->next;
                    pNode->next = tmp_1;
                    tmp_1 = tmp_2;
                    count++;
                }
                tmp_tail->next = tmp_1;
                break;
            }
            pNode = pNode->next;
        }
    }
    else{
        tmp_tail = head;
        while(n > count){
            tmp_2 = head->next;
            head->next = tmp_head;
            tmp_head = head;
            head = tmp_2;
            count++;
        }
        tmp_tail->next = head;
        head = tmp_head;
    }
    return head;
}
时间: 2024-08-29 06:41:06

LeetCode OJ 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. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

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

【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

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

[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 OJ:Reverse Linked List (反转链表)

Reverse a singly linked list. 做II之前应该先来做1的,这个导师很简单,基本上不用考虑什么,简单的链表反转而已: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10

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