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

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

相比翻转整个链表稍微麻烦了那么一点,不过只要考虑好翻转的具体实现,问题都一样。AC代码如下:

    ListNode* reverse(ListNode* head,int len) {
        if(head == NULL || len<=0)
            return head;
        ListNode *first =head;
        ListNode *wait = head->next;
        ListNode *wait_next = NULL;
        first->next= NULL;
        for(;wait!=NULL,len>0;--len)
        {
            wait_next = wait->next;
            wait->next = head;
            head = wait;
            wait = wait_next;
        }
        if(len>0)
            return head;
        else
        {
            first->next = wait;
            return head;
        }
    }

    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if(head ==NULL || m>=n)
            return head;
        if(m==1)
            return reverse(head,n-m);
        ListNode* h=head;
        ListNode* fir=head;
        int len=n-m;
        while(m>1)
        {
            fir=h;
            h=h->next;
            if(h==NULL)
                return head;
            m=m-1;
        }
        //第m个节点在链表中,且h指向该节点
        fir->next=reverse(h,len);
        return head;
    }
时间: 2024-08-28 01:52:59

LeetCode【92】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 condition:1 ≤ m ≤ n ≤ l

【LeetCode92】Reverse Linked List II★★

题目描述: 解题思路: 题目大意:给定一个链表,反转第m到第n个结点部分,m.n满足1 ≤ m ≤ n ≤ length of list. 解题思路参照LeetCode206题,用迭代法,不过要注意以下几点: (a):为方便操作,新建一个辅助结点dummy,使其下一个结点指向头节点. (b):维护4个指针:pre.current.then.then_next,pre指向第m个结点的前一个结点,current指向当前操作的位于区间[m,n]的结点,then指向current的下一个结点,then_

【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 ≤ lengt

【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 ≤ lengt

【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

【leetcode】Reverse Linked List II (middle)

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了.但写的很罗嗦,要学习大神的写法. 注意翻转的写法. 用伪头部 大神14行简洁代码 L

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

LeetCode【7】.Reverse Integer--java实现

Reverse Integer 题目要求:给定一个int 类型值,求值的反转,如下: Example1: x = 123, return 321 Example2: x = -123, return -321 简单问题一般蕴含细节的处理.思路很简单,直接贴Java程序: public class Solution { public int reverse(int x) { int head = x/10; int tail = x%10; long re = 0; while(head!=0||