当前位置:网站首页>(practice C language every day) reverse linked list II
(practice C language every day) reverse linked list II
2022-07-06 06:34:00 【Yi Xiaoxia】
Reverse a linked list II
Give you the head pointer of the single linked list head
And two integers left
and right
, among left <= right
. Please reverse from position left
To the position right
The linked list node of , return Inverted list .
Example 1:
Input :head = [1,2,3,4,5], left = 2, right = 4 Output :[1,4,3,2,5]
Example 2:
Input :head = [5], left = 1, right = 1 Output :[5]
Tips :
- The number of nodes in the linked list is
n
1 <= n <= 500
-500 <= Node.val <= 500
1 <= left <= right <= n
The following program realizes this function :
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
static struct ListNode *reverseBetween(struct ListNode *head, int m, int n)
{
int i;
struct ListNode dummy;
struct ListNode *prev = &dummy;
prev->next = head;
for (i = 1; i < m; i++)
{
prev = prev->next;
}
struct ListNode *p = prev->next;
for (i = m; i < n; i++)
{
struct ListNode *q = p->next;
p->next = q->next;
q->next = prev->next;
prev->next = q;
}
return dummy.next;
}
int main(int argc, char **argv)
{
if (argc < 3)
{
fprintf(stderr, "Usage: ./test m n 1 2 3...\n");
exit(-1);
}
int i, count = argc - 3;
struct ListNode dummy;
struct ListNode *prev = &dummy;
struct ListNode *p;
for (i = 0; i < count; i++)
{
p = malloc(sizeof(*p));
p->val = atoi(argv[i + 3]);
p->next = NULL;
prev->next = p;
prev = p;
}
int m = atoi(argv[1]);
int n = atoi(argv[2]);
struct ListNode *head = reverseBetween(dummy.next, m, n);
for (p = head; p != NULL; p = p->next)
{
printf("%d ", p->val);
}
printf("\n");
return 0;
}
边栏推荐
- Full link voltage measurement: building three models
- 私人云盘部署
- 模拟卷Leetcode【普通】1296. 划分数组为连续数字的集合
- [ 英语 ] 语法重塑 之 英语学习的核心框架 —— 英语兔学习笔记(1)
- 关于新冠疫情,常用的英文单词、语句有哪些?
- LeetCode 739. Daily temperature
- ECS accessKey key disclosure and utilization
- Grouping convolution and DW convolution, residuals and inverted residuals, bottleneck and linearbottleneck
- 如何将flv文件转为mp4文件?一个简单的解决办法
- Simulation volume leetcode [general] 1296 Divide an array into a set of consecutive numbers
猜你喜欢
MySQL5.72.msi安装失败
在uni-app中使用腾讯视频插件播放视频
mysql按照首字母排序
Selenium source code read through · 9 | desiredcapabilities class analysis
英语论文翻译成中文字数变化
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
On weak network test of special test
Past and present lives of QR code and sorting out six test points
云服务器 AccessKey 密钥泄露利用
How effective is the Chinese-English translation of international economic and trade contracts
随机推荐
Summary of the post of "Web Test Engineer"
Private cloud disk deployment
Summary of leetcode's dynamic programming 4
Py06 字典 映射 字典嵌套 键不存在测试 键排序
国际经贸合同翻译 中译英怎样效果好
Simulation volume leetcode [general] 1296 Divide an array into a set of consecutive numbers
Simulation volume leetcode [general] 1109 Flight reservation statistics
模拟卷Leetcode【普通】1296. 划分数组为连续数字的集合
The pit encountered by keil over the years
Black cat takes you to learn UFS protocol Chapter 18: how UFS configures logical units (Lu Management)
Simulation volume leetcode [general] 1414 The minimum number of Fibonacci numbers with a sum of K
Fledgling Xiao Li's 103rd blog CC2530 resource introduction
英语论文翻译成中文字数变化
Simulation volume leetcode [general] 1061 Arrange the smallest equivalent strings in dictionary order
How much is it to translate Chinese into English for one minute?
Modify the list page on the basis of jeecg boot code generation (combined with customized components)
org. activiti. bpmn. exceptions. XMLException: cvc-complex-type. 2.4. a: Invalid content beginning with element 'outgoing' was found
Simulation volume leetcode [general] 1405 Longest happy string
Lecture 8: 1602 LCD (Guo Tianxiang)
sourceInsight中文乱码