- 浏览: 183480 次
- 性别:
- 来自: 济南
文章分类
最新评论
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的位置,然后依次交换位置,代码如下:
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; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
进行一次遍历,把第m到n个元素进行翻转,即依次插入到第m个节点的头部。这个题还是有意思的。建议后面再多做几遍。Python代码如下:self.next = No
在计算机科学领域,数据结构是基础且至关重要的概念,它为高效地存储和处理数据提供了方法。本话题聚焦于一种常见的线性数据结构——单链表,并探讨如何对其进行逆序输出。逆序输出单链表涉及到对链表节点顺序的反转...
javascript js_leetcode题解之92-reverse-linked-list-ii.js
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 ...
c语言基础 c语言_leetcode题解之0092_reverse_linked_list_ii.zip
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→...
java入门 java_leetcode题解之206_Reverse_Linked_List
17. **Reverse Linked List II**:在链表中删除指定范围内的节点并反转剩余部分。需要保持原有结构,同时进行翻转操作。 18. **Rotate List**:旋转链表,例如将链表的最后k个节点移动到前面。通过计算链表长度和...
c++ C++_leetcode题解之206_Reverse_Linked_List.cpp
python python_leetcode题解之206_Reverse_Linked_List.py
第92题“反转链表II”(Reverse Linked List II)要求对链表的一部分进行反转。具体来说,给定一个链表的头节点`head`、一个整数`m`和一个整数`n`,你需要反转从位置`m`到位置`n`的链表部分,其中位置`1`为链表的...
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
* [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...
2. 反转链表(Reverse Linked List) 反转链表是将链表的方向反转,即将链表的头节点变为尾节点,尾节点变为头节点。这种操作可以用于解决一些特殊的问题,例如,反转链表以满足某些算法的要求。 知识点:反转链表...
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)算法与源代码 递归算法的执行过程分递推和回归两个阶段。在递推阶段,把较复杂的问题(规模为n)的求解推到比原问题简单一些的问题(规模小于n)的...
`Reverse`方法则需要两个指针,一个记录当前节点,另一个记录前一个节点,依次交换节点的`Next`指针,直到遍历完整个链表。 项目的实现细节可能包含异常处理,如确保插入和删除操作时不会超出链表范围,或者在空...
本课件“Chapter4 Array and Linked list Exercise”聚焦于两种基础且重要的数据结构:数组(Array)和链表(Linked list),这些都是编程中不可或缺的基础知识。 1. **数组**: 数组是一种线性数据结构,它包含...