[Leetcode]Reverse Linked List II
2023-09-27 14:27:00 时间
//将list存入vector。然后翻转中间部分数列
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
vector<ListNode*> node;
ListNode* cur = head;
for(int i=0;i<m-1;i++)
cur=cur->next;
for(int i=0;i<=n-m;i++)
{
node.push_back(cur);
cur=cur->next;
}
for(int i=0;i<(n-m+1)/2;i++)
swap(node[i]->val,node[node.size()-i-1]->val);
return head;
}
};
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
vector<ListNode*> node;
ListNode* cur = head;
for(int i=0;i<m-1;i++)
cur=cur->next;
for(int i=0;i<=n-m;i++)
{
node.push_back(cur);
cur=cur->next;
}
for(int i=0;i<(n-m+1)/2;i++)
swap(node[i]->val,node[node.size()-i-1]->val);
return head;
}
};
相关文章
- 矩阵中和能被 K 整除的路径 leetcode第314周赛第四题
- LeetCode_二分搜索_双指针_中等_287.寻找重复数
- LeetCode_回溯算法_动态规划_简单_104.二叉树的最大深度
- LeetCode·每日一题·795.区间子数组个数·脑筋急转弯
- leetcode 234: Palindrome Linked List
- LeetCode——Rotate List
- Reverse Linked List II -- LeetCode
- leetcode先刷_Remove Duplicates from Sorted List II
- LeetCode || Copy List with Random Pointer
- LeetCode-38. 外观数列(Goland实现)
- [LeetCode] 567. Permutation in String 字符串中的全排列
- [LeetCode] 245. Shortest Word Distance III 最短单词距离 III
- [LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树
- [LeetCode] 148. Sort List 链表排序
- [LeetCode] 477. Total Hamming Distance 全部汉明距离
- [LeetCode] 203. Remove Linked List Elements 移除链表元素
- [LeetCode] 234. Palindrome Linked List 回文链表
- [LeetCode] 341. Flatten Nested List Iterator 压平嵌套链表迭代器
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
- [LeetCode] 48. Rotate Image 旋转图像
- [LeetCode] 206. Reverse Linked List 反向链表
- [LeetCode] 138. Copy List with Random Pointer 拷贝带随机指针的链表
- leetcode 922 按奇偶排序数组II
- leetcode 860柠檬水找零