LeeCode92. 反转链表II

给你单链表的头指针 head 和两个整数 leftright ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表

示例 1:

复制代码
输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]

示例 2:

复制代码
输入:head = [5], left = 1, right = 1
输出:[5]

提示:

  • 链表中节点数目为 n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

进阶: 你可以使用一趟扫描完成反转吗?

答案&测试代码:

cpp 复制代码
void testLeeCode92(void) { // LeeCode92.反转链表II
	struct ListNode {
		int val;
		ListNode* next;
		ListNode() : val(0), next(nullptr) {}
		ListNode(int x) : val(x), next(nullptr) {}
		ListNode(int x, ListNode* next) : val(x), next(next) {}
	};

	class Solution {
	private:
		ListNode* reverseList(ListNode* head) { // 反转整个链表
			if (!head) return nullptr;
			ListNode* next = head->next;
			if (!next) {
				return head;
			}
			head->next = nullptr; // 断开联系
			ListNode* newHead = reverseList(next); // 将后面的链表排序好
			next->next = head; // 该节点添加到最后
			return newHead;
		}

	public:
		ListNode* reverseBetween(ListNode* head, int left, int right) { // LeeCode92.反转链表II
			// 将整个链表拆分为3部分,中间那部分用于反转
			ListNode *fistHead = nullptr; // 第一段链表的头节点
			ListNode *firstLast = nullptr; // 第一段链表的尾节点

			// 把第一段链表拆出来
			for (int i = 1; i < left; i++) {
				if (!fistHead) {
					fistHead = head;
				}
				if (!firstLast) {
					firstLast = head;
				}
				else {
					firstLast = firstLast->next;
				}
			}
			
			// 再把中间要反转的那段链表拆出来
			ListNode* midFirst = firstLast? firstLast->next : head;
			if (firstLast) {
				firstLast->next = nullptr;
			}
			ListNode* midLast = midFirst;
			int midLen = right - left + 1;
			for (int i = 1; i < midLen; i++) {
				midLast = midLast->next;
			}

			// 再把最后一段链表拆出来
			ListNode* foot = midLast? midLast->next : nullptr;
			if (midLast)
				midLast->next = nullptr;

			// 再反转中间那部分
			ListNode *mid_ = reverseList(midFirst);

			// 再拼接
			ListNode* newHead = mid_;
			if (firstLast) {
				firstLast->next = newHead;
				newHead = fistHead;
			}
			if (foot && midFirst) {
				midFirst->next = foot; // // 中间部分反转后,尾节点为原来的头节点
			}
			return newHead;
		}
	};

	Solution solution;
	ListNode node1 = ListNode(1);
	ListNode node2 = ListNode(2);
	ListNode node3 = ListNode(3);
	ListNode node4 = ListNode(4);
	ListNode node5 = ListNode(5);
	node1.next = &node2;
	node2.next = &node3;
	node3.next = &node4;
	node4.next = &node5;
	ListNode* res = solution.reverseBetween(&node1, 2, 4);

	// 打印链表
	std::cout << "链表:";
	for (ListNode* p = res; p;p = p->next) {
		std::cout << p->val << " ";
	}
	std::cout << endl;
}

打印:

ok. 提交到LeeCode:

ok.

相关推荐
2401_841495643 小时前
【数据结构】汉诺塔问题
java·数据结构·c++·python·算法·递归·
xxxxxxllllllshi3 小时前
Java 集合框架全解析:从数据结构到源码实战
java·开发语言·数据结构·面试
Q741_1473 小时前
C++ 位运算 高频面试考点 力扣137. 只出现一次的数字 II 题解 每日一题
c++·算法·leetcode·面试·位运算
天特肿瘤电场研究所3 小时前
专业的肿瘤电场疗法厂家
算法
DASXSDW3 小时前
NET性能优化-使用RecyclableBuffer取代RecyclableMemoryStream
java·算法·性能优化
kfepiza4 小时前
CAS (Compare and Swap) 笔记251007
java·算法
墨染点香4 小时前
LeetCode 刷题【103. 二叉树的锯齿形层序遍历、104. 二叉树的最大深度、105. 从前序与中序遍历序列构造二叉树】
算法·leetcode·职场和发展
啊我不会诶4 小时前
23ICPC澳门站补题
算法·深度优先·图论
Brookty5 小时前
【算法】二分查找(一)朴素二分
java·学习·算法·leetcode·二分查找