LeetCode: Reverse Linked List

LeetCode: Reverse Linked List

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

地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/

算法:先找到第m个节点p以及其前趋pre,然后把p记下,因为p是逆置链表部分的最后一个节点。然后从p开始遍历到第n个节点,并把节点依次插入pre后面,注意处理pre为空,也就是m=1的情况。代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *reverseBetween(ListNode *head, int m, int n) {
12         if(!head)   return NULL;
13         ListNode *pre = NULL;
14         ListNode *p = head;
15         int i = 1;
16         while(p && i != m){
17             pre = p;
18             p = p->next;
19             ++i;
20         }
21         if(!p)  return head;
22         ListNode *first_reverse_node = p;
23         if(pre){
24             pre->next = NULL;
25         }
26         ListNode *q = p;
27         while(q && i <= n){
28             q = p->next;
29             if(pre){
30                 p->next = pre->next;
31                 pre->next = p;
32             }else{
33                 if(p == head)
34                     head->next = NULL;
35                 else{
36                     p->next = head;
37                     head = p;
38                 }
39             }
40             p = q;
41             ++i;
42         }
43         first_reverse_node->next = q;
44         return head;
45     }
46 };
时间: 2024-10-07 05:06:34

LeetCode: Reverse Linked List的相关文章

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

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

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

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 fol

LeetCode: Reverse Linked List II [092]

[题目] 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 单链表区间范围内逆置

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

(每日算法)LeetCode --- Reverse Linked List II(旋转链表的指定部分)

Reverse Linked List II(旋转链表的指定部分) Leetcode 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 sati

[LeetCode] Reverse Linked List 倒置链表

Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 之前做到Reverse Linked List II 倒置链表之二的时候我还纳闷怎么只有二没有一呢,原来真是忘了啊,现在才加上,这道题跟之前那道比起来简单不少,题目为了增加些许难度,让我们分别用

[LeetCode] Reverse Linked List II @ Python

原题地址:https://oj.leetcode.com/problems/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.

(leetcode)Reverse Linked List 脑子已经僵住

Reverse a singly linked list. 参考http://www.2cto.com/kf/201110/106607.html 方法1: 讲每个节点的指针指向前面就可以. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution