`

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.

用一个辅助节点helper找到m的位置,然后依次交换位置,代码如下:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode helper = new ListNode(0);
        helper.next = head;
        head = helper;
        int k = n - m;
        while(m > 1) {
            head = head.next;
            m --;
        }
        ListNode reve = head.next;
        while(k > 0) {
            ListNode cur = reve.next;
            reve.next = cur.next;
            cur.next = head.next;
            head.next = cur;
            k --;
        }
        return helper.next;
    }
}
0
0
分享到:
评论

相关推荐

    fuxuemingzhu#Leetcode-Solution-All#92. Reverse Linked List II 反转

    进行一次遍历,把第m到n个元素进行翻转,即依次插入到第m个节点的头部。这个题还是有意思的。建议后面再多做几遍。Python代码如下:self.next = No

    linked-list-reverse-output.rar_Reverse Linked List

    数据结构,单链表 ,逆序输出,数据结构,单链表 ,逆序输出

    leetcode盒子嵌套-leetcode-text:leetcode-文本

    92.Reverse Linked List II Runtime: 4 ms, faster than 67.04% of C online submissions for Reverse Linked List II. Memory Usage: 6.9 MB, less than 100.00% of C online submissions for Reverse Linked List ...

    cpp-算法精粹

    Reverse Linked List II Partition List Remove Duplicates from Sorted List Remove Duplicates from Sorted List II Rotate List Remove Nth Node From End of List Swap Nodes in Pairs Reverse Nodes in k-Group...

    leetcode中文版-LeetCode:力码

    92.Reverse Linked List II LeetCode 138.Copy List with Random Pointer LeetCode 142.Linked List Cycle II(solve1) LeetCode 142.Linked List Cycle II(solve2) LeetCode 160.Intersection of Two Linke

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    陈越、何钦铭-数据结构作业6:Reversing Linked List链表翻转

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→...

    leetcodepython001-Algorithm:关于数据结构和算法的问题(Leetcode、编程之美和课程作业)

    leetcode python 001 Algorithm 用python和java解决了Leetcode上部分问题,另外还有对常规算法和机器学习算法...Linked List II Python 2017/11/28 Medium 095 Unique Binary Search Trees Python 2017/11/28 Medium 09

    C#,递归方法实现双向链表(Doubly Linked List)的反转(Reverse)算法与源代码

    C#,递归方法实现双向链表(Doubly Linked List)的反转(Reverse)算法与源代码 递归算法的执行过程分递推和回归两个阶段。在递推阶段,把较复杂的问题(规模为n)的求解推到比原问题简单一些的问题(规模小于n)的...

    leetcode不会-Leetcode-Java:Leetcode-Java

    leetcode 不会 Leetcode Solutions in Java Linked List Linked List ...linked list, ...快慢指针法,块指针从head.next开始,慢指针从head开始,快指针每次移动两格,慢...reverseList(ListNode head) 三个指针,依次往后

    leetcode2sumc-LeetCode:LeetCode的一些题目

    leetcode 2 sum c LeetCode 帮助文档 帮助文档存放在Help文件夹下。 文件名 文件描述 ...complexitypython.txt Python的一些常规操作的复杂度统计 ...Reverse Linked List 234 Easy Palindrome Linked List

    Leetcode 反转链表.js

    LeetCode 206的题目是“反转链表”(Reverse Linked List),它要求将一个单链表的所有节点反转,并返回反转后链表的头节点。这是一个基础但非常重要的链表操作问题,它不仅考察了对链表数据结构的理解,还涉及到了...

    LeetCode2 Add Two Numbers

    You are given two non-empty linked lists ... Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. java AC版本

    leetcode中325题python-leetcode:leetcode

    reverse-linked-list-ii(Reverse a Sub-list) 141 环形链表 linked-list-cycle 142 环形链表 II linked-list-cycle-ii 143 重排链表 reorder-list 148 排序链表 sort-list 234 回文链表 palindrome-linked-list 双...

    leetcode分发糖果-ForDaFactory:使用C++的个人leetcode解决方案

    92-反转链表II:reverse-linked-listt-ii 141-环形链表:linked-list-cycle 142-环形链表:linked-list-cycle-ii 160-相交链表:intersection-of-two-linked-lists 206-反转一个单链表:reverse-linked-list 20-有效的...

    tech.github.io:我的博客

    终生成长 :hot_beverage: 为什么要建这个仓库 梳理自己掌握的知识点,整理自己的知识体系。... Reverse Linked ListLeetcode 141. Linked List CycleLeetcode 21. Merge Two Sorted ListsLeetCode 224. Basic Cal

    Reverse-A-Linked-List:动画蛇游戏,可视化反转链表的算法

    关于 这个项目包含了一个蛇游戏,但是有点扭曲。 在这里,我已经实现了反向链接算法,并且可以类似地用于可视化相同的算法。 可用脚本 在项目目录中,可以运行: npm start 在开发模式下运行该应用程序。...

    单链表双向链表ADT_C语言项目

    6. ReverseList 7. IsLoopList 8. ReverseEvenList 9. FindMidNode Double Linked List: 1. DestroyList_DuL 2. InsertBeforeList_DuL 3. InsertAfterList_DuL 4. DeleteList_DuL 5. TraverseList_DuL

    leetcode跳跃-LeetCode:力扣刷题70道!

    Reverse Linked List 队列 Queue 力扣 933 最近的请求次数 | Number of Recent Calls 力扣 225 用队列实现栈 | Implement Stack Using Queue 力扣 622 设计循环队列 | Design Circular Queue 力扣 641 设计循环双端...

    build_linked_list:构建链接列表。 方法

    链接列表 构建链接列表。 方法:reverseLinkedList,getKthFromTheEnd,toArray,大小,indexOf,包含,deleteLast,deleteFirst,addFirst,addLast

Global site tag (gtag.js) - Google Analytics