[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->N

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.

解题思路

解题思路与上一篇Reverse Linked List类似,只不过这里是反转部分链表。
因此,只需将m到n部分翻转,然后将m的前缀beforeM指向该部分反转后的头结点即可。如果beforeM为NULL,则m=1,m到n部分的头结点即为链表的头结点。

实现代码

//Runtime: 4 ms
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

struct ListNode{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        ListNode *cur = head;
        ListNode *beforeM = NULL;
        for (int i = 1; i < m; i++)
        {
            beforeM = cur;
            cur = cur->next;
        }

        ListNode *localhead = cur;
        ListNode *pre = cur;
        cur = cur->next;
        int count = n - m;
        while (count--)
        {
            pre->next = cur->next;
            cur->next = localhead;
            localhead = cur;
            cur = pre->next;
        }

        if (beforeM == NULL)
        {
            head = localhead;
        }
        else
        {
            beforeM->next = localhead;
        }

        return head;
    }
};

void printList(ListNode *head)
{
    while(head != NULL)
    {
        cout<<head->val<<" ";
        head = head->next;
    }
    cout<<endl;
}

void dropList(ListNode *head)
{
    if (head == NULL) return;
    ListNode *temp;
    while (head != NULL)
    {
        temp = head->next;
        delete head;
        head = temp;
    }
}

int main()
{
    ListNode *head = new ListNode(0);
    ListNode *cur = head;
    for (int i = 0; i < 10; i++)
    {
        ListNode *newnode = new ListNode(floor(rand()%77));
        cur->next = newnode;
        cur = newnode;
    }
    printList(head);
    Solution s;
    head = s.reverseBetween(head, 2, 5);
    printList(head);
    dropList(head);
}
目录
相关文章
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
46 1
|
C++
Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的具体讲解在《剑指offer》的P149页有思路讲解,如果你手头有这本书,建议翻阅。
51 0
Leetcode 19.Remove Nth Node From End of List
删除单链表中的倒数第n个节点,链表中删除节点很简单,但这道题你得先知道要删除哪个节点。在我的解法中,我先采用计数的方式来确定删除第几个节点。另外我在头节点之前额外加了一个节点,这样是为了把删除头节点的特殊情况转换为一般情况,代码如下。
39 0
|
7月前
|
存储 Python
链表(Linked List)详解
链表(Linked List)详解
51 0
|
7月前
|
大数据 Java 程序员
「LeetCode合集」链表(List)及经典问题
「LeetCode合集」链表(List)及经典问题
53 0
|
C++
【PAT甲级 - C++题解】1133 Splitting A Linked List
【PAT甲级 - C++题解】1133 Splitting A Linked List
83 0
|
C++
【PAT甲级 - C++题解】1074 Reversing Linked List
【PAT甲级 - C++题解】1074 Reversing Linked List
73 0
|
机器学习/深度学习 存储 C++
【PAT甲级 - C++题解】1052 Linked List Sorting
【PAT甲级 - C++题解】1052 Linked List Sorting
84 0
二叉树(Binary Tree)的二叉链表(Binary Linked List)实现
二叉树(Binary Tree)的二叉链表(Binary Linked List)实现
|
存储 C++
线性表的链式存储结构 单链表(Singly Linked List) C++
线性表的链式存储结构 单链表(Singly Linked List) C++