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

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

Note:
Given mn satisfy
the following condition:
1 ≤ m ≤ n ≤
length of list.


public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if(m==n||m>n)
return head;
int len = n-m;
ListNode root = head;
m--;
while(m!=0&&root!=null){
root = root.next;
m--;
}
ListNode cur = root;
while(len!=0&&len>0){
ListNode temp = cur;
int i=len;
while(i!=0){
temp=temp.next;
i--;
}

int tempv = cur.val;
cur.val=temp.val;
temp.val=tempv;
len-=2;
cur=cur.next;
}

return head;

}
}

【LeetCode】Reverse Linked List II,布布扣,bubuko.com

时间: 2024-11-02 23:21:17

【LeetCode】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

【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

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. 相比翻转整个链表稍微麻烦了那么一点,不过只要考虑好翻转的具体实现,问题都一样.AC代码如下: ListNode* reverse(

【Leetcode】Reverse Linked List

原题链接:https://leetcode.com/problems/reverse-linked-list/ 题目: Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 思路: 头插法建立链表 算法: [java] view plain copy public ListNode reverseL

【链表】 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

【leetcode】Reverse Linked List(easy)

Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList = NULL, * tmp = NULL; while(head != NULL) { tmp = rList; rList = head; head = head->next; rList->next = tmp; } return rList; }

【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】链表 linked list(共34题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 =

【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