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

【题意】

将给定链表第m个节点到第n个节点的位置逆序,返回逆序后的链表。
给定M,N满足以下条件: 
1≤M≤N≤列表的长度。

【分析】

思路1:

前m-1个不变,从第m+1个到第n个,依次删除,用尾插法插入到第m-1个节点后面。

    第一步把4节点删除放入2节点之后

 第二步把5节点删除放入2节点之后

【代码1】

/*********************************
*   日期:2014-01-28
*   作者:SJF0115
*   题号: Reverse Linked List II
*   来源:http://oj.leetcode.com/problems/reverse-linked-list-ii/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <algorithm>
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) {
        if(m > n || n < 0){
            return head;
        }
        ListNode *tail,*p,*rTail,*pre = NULL;
        //添加虚拟头结点(便于反转全部)
        ListNode *beginNode = (ListNode*)malloc(sizeof(ListNode));
        beginNode->next = head;
        pre = beginNode;

        int index = 1;
        //遍历前m-1个节点
        while(pre != NULL && index < m){
            pre = pre->next;
            index++;
        }
        tail = pre;
        rTail = pre->next;
        index = 1;
        //删除第m+1节点开始
        while(index < (n-m+1) ){
            //删除p节点
            p = rTail->next;
            rTail->next = p->next;
            //尾插法
            p->next = tail->next;
            tail->next = p;
            index++;
        }
        return beginNode->next;
    }
};
int main() {
    Solution solution;
    int A[] = {1,2,3,4,5,6,7,8,9};
    ListNode *head = (ListNode*)malloc(sizeof(ListNode));
    head->next = NULL;
    ListNode *node;
    ListNode *pre = head;
    for(int i = 0;i < 1;i++){
        node = (ListNode*)malloc(sizeof(ListNode));
        node->val = A[i];
        node->next = NULL;
        pre->next = node;
        pre = node;
    }
    head = solution.reverseBetween(head->next,1,8);
    while(head != NULL){
        printf("%d ",head->val);
        head = head->next;
    }
    return 0;
}

【代码2】

class Solution {
public:
    ListNode *reverseBetween(ListNode *head, int m, int n) {
        ListNode dummy(0);
        dummy.next = head;

        ListNode *preM, *pre = &dummy;
        for (int i = 1; i <= n; ++i) {
            //preM 第m-1个节点
            if (i == m) preM = pre;
            if (i > m && i <= n) {
                //删除head节点
                pre->next = head->next;
                head->next = preM->next;
                //尾插法
                preM->next = head;
                head = pre; // head has been moved, so pre becomes current
            }
            pre = head;
            head = head->next;
        }
        return dummy.next;
    }
};
时间: 2024-10-22 13:13:15

[LeetCode]92.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 following condition: 1 ≤ m ≤ n ≤ le

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(递归与非递归反转链表)

Reverse a singly linked list. 解题思路 对于非递归实现,思路是依次将从第二个结点到最后一个结点的后继设为头结点,然后将该节点设为头结点(需记住将原头结点的后继设为空). 对于递归实现,首先反转从第二个结点到最后一个结点的链表,然后再将头结点放到已反转链表的最后,函数返回新链表的头结点. 非递归实现代码1[C++] //Runtime:10 ms class Solution { public: ListNode* reverseList(ListNode* head

[LeetCode]25.Reverse Nodes in k-Group

[题目] Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, onl

[LeetCode]203.Remove Linked List Elements

题目 Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5 思路 链表节点的删除操作. 代码 /*--------------------------------------- * 日期:

[LeetCode]234.Palindrome Linked List

题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 思路 利用双指针法找到链表中点位置,链表中点以后的的元素(不包括中点元素)翻转,再跟链表中点位置以前的元素一一匹配. 代码 /*--------------------------------------- * 日期:2015-07-18 * 作者:SJF01

LeetCode 7 Reverse Integer(翻转整数)

翻译 翻转一个整型数 例1:x = 123, 返回 321 例2:x = -123, 返回 -321 原文 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? (来自LeetCode官网) Here are some good questions to ask before coding. Bonus poi

[LeetCode]151.Reverse Words in a String

题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. click to show clarification.

LeetCode 25 Reverse Nodes in k-Group(在K组链表中反转结点)

原文 给定一个链表,在一定时间内反转这个链表的结点,并返回修改后的链表. 如果结点数不是K的倍数,那么剩余的结点就保持原样. 你不应该在结点上修改它的值,只有结点自身可以修改. 只允许使用常量空间. 例如 给定链表: 1->2->3->4->5 对于 k = 2,你应该返回: 2->1->4->3->5 对于 k = 3,你应该返回: 3->2->1->4->5 翻译 Given a linked list, reverse the