当前位置: 首页 > news >正文

LeetCode //C - 143. Reorder List

143. Reorder List

You are given the head of a singly linked-list. The list can be represented as:

L0 → L1 → … → Ln - 1 → Ln
Reorder the list to be on the following form:

L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
You may not modify the values in the list’s nodes. Only nodes themselves may be changed.
 

Example 1:

在这里插入图片描述

Input: head = [1,2,3,4]
Output: [1,4,2,3]

Example 2:

在这里插入图片描述

Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]

Constraints:
  • The number of nodes in the list is in the range [ 1 , 5 ∗ 1 0 4 ] [1, 5 * 10^4] [1,5104].
  • 1 <= Node.val <= 1000

From: LeetCode
Link: 143. Reorder List


Solution:

Ideas:
  1. Find the middle of the linked list: We can use the fast and slow pointer technique to find the middle node.
  2. Reverse the second half of the linked list: Once we find the middle, we need to reverse the second half of the list.
  3. Merge the two halves: Finally, we merge the two halves by alternating nodes from the first half and the reversed second half.
Code:
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
void reorderList(struct ListNode* head) {if (!head || !head->next) return;// Step 1: Find the middle of the linked liststruct ListNode *slow = head, *fast = head;while (fast->next && fast->next->next) {slow = slow->next;fast = fast->next->next;}// Step 2: Reverse the second half of the liststruct ListNode *prev = NULL, *curr = slow->next, *next = NULL;while (curr) {next = curr->next;curr->next = prev;prev = curr;curr = next;}slow->next = NULL; // Cut the list into two halves// Step 3: Merge the two halvesstruct ListNode *first = head, *second = prev;while (second) {struct ListNode *tmp1 = first->next, *tmp2 = second->next;first->next = second;second->next = tmp1;first = tmp1;second = tmp2;}
}// Helper function to create a new ListNode
struct ListNode* newNode(int val) {struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));node->val = val;node->next = NULL;return node;
}// Helper function to print the linked list
void printList(struct ListNode* head) {while (head) {printf("%d -> ", head->val);head = head->next;}printf("NULL\n");
}
http://www.lryc.cn/news/356722.html

相关文章:

  • 速盾:cdn如何解析?
  • K8s集群调度续章
  • 大工作量LUAD代谢重编程模型多组学(J Transl Med)
  • C语言#include<>和#include““有什么区别?
  • 正在直播:Microsoft Copilot Studio 新增支持Copilot代理、Copilot扩展等多项功能
  • 数据通信基本概念汇总
  • AcWing 835. Trie字符串统计——算法基础课题解
  • RT-DETR算法改进【NO.1】借鉴CVPR2024中的StarNet网络StarBlock改进算法
  • 5,串口编程---实现简单的用串口发送接收数据
  • LeetCode583:两个字符串的删除操作
  • LLama学习记录
  • 如何克隆非默认分支
  • 数据结构——图
  • 蓝桥杯—SysTick中断精准定时实现闪烁灯
  • ML307R OpenCPU UDP使用
  • pod详解
  • 免费插件集-illustrator插件-Ai插件-文本对象分行
  • web学习笔记(五十九)
  • UE5 UE4 快速定位节点位置
  • go routing 之 gorilla/mux
  • 新火种AI|警钟长鸣!教唆自杀,威胁人类,破坏生态,AI的“反攻”值得深思...
  • AAA实验配置
  • Maven高级详解
  • C++的算法:模拟算法
  • Spring boot集成easy excel
  • 【开发 | 环境配置】解决 VSCode 编写 eBPF 程序找不到头文件
  • View->Bitmap缩放到自定义ViewGroup的任意区域
  • 十种常用数据分析方法
  • 拉格朗日插值及牛顿差商方法的实现(Matlab)
  • 【InternLM实战营第二期笔记】02:大模型全链路开源体系与趣味demo