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 *root = new ListNode(0);
        root->next = head;
        ListNode *mNode = new ListNode(0);
        ListNode *pre = root , *curr = head;
        for(int i = 1; i <= n; i ++) {
            if(i == m) mNode = curr;  //记录第m个结点
            if(i < m) pre = curr;     //记录第m-1个结点
            ListNode *next = curr->next;
            if(i > m && i <= n) {
                mNode->next = next;   //避免链表断裂
                curr->next = pre->next;
                pre->next = curr;
            }
            curr = next;
        }
        return root->next;
    }
};

  

时间: 2024-11-07 03:15:58

92:Reverse Linked List II翻转链表【链表】的相关文章

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 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 Reverse Linked List II 反置链表2

题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交给DFS解决,用个计数器来统计已经反置的个数即可. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : va

[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

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的位置(开始反转位置的前一个节

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

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