当前位置:网站首页>(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 <= 5001 <= 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;
}边栏推荐
- 如何做好互联网金融的英语翻译
- Delete the variables added to watch1 in keil MDK
- PHP uses redis to implement distributed locks
- University of Manchester | dda3c: collaborative distributed deep reinforcement learning in swarm agent systems
- LeetCode 1200. Minimum absolute difference
- Summary of leetcode's dynamic programming 4
- 钓鱼&文件名反转&office远程模板
- 专业论文翻译,英文摘要如何写比较好
- org.activiti.bpmn.exceptions.XMLException: cvc-complex-type.2.4.a: 发现了以元素 ‘outgoing‘ 开头的无效内容
- 中英对照:You can do this. Best of luck祝你好运
猜你喜欢

Data type of MySQL

In English translation of papers, how to do a good translation?

专业论文翻译,英文摘要如何写比较好

LeetCode 731. My schedule II

How much is it to translate Chinese into English for one minute?

org.activiti.bpmn.exceptions.XMLException: cvc-complex-type.2.4.a: 发现了以元素 ‘outgoing‘ 开头的无效内容

Phishing & filename inversion & Office remote template

MFC关于长字符串unsigned char与CString转换及显示问题

端午节快乐Wish Dragon Boat Festival is happy

中英对照:You can do this. Best of luck祝你好运
随机推荐
在JEECG-boot代码生成的基础上修改list页面(结合自定义的组件)
论文摘要翻译,多语言纯人工翻译
Simulation volume leetcode [general] 1218 Longest definite difference subsequence
Construction and integration of Zipkin and sleuth for call chain monitoring
On weak network test of special test
Set the print page style by modifying style
Cobalt strike feature modification
sourceInsight中文乱码
Simulation volume leetcode [general] 1109 Flight reservation statistics
模拟卷Leetcode【普通】1091. 二进制矩阵中的最短路径
Simulation volume leetcode [general] 1447 Simplest fraction
Cobalt Strike特征修改
Simulation volume leetcode [general] 1143 Longest common subsequence
Private cloud disk deployment
Py06 dictionary mapping dictionary nested key does not exist test key sorting
Simulation volume leetcode [general] 1249 Remove invalid parentheses
中英对照:You can do this. Best of luck祝你好运
专业论文翻译,英文摘要如何写比较好
翻译生物医学说明书,英译中怎样效果佳
模拟卷Leetcode【普通】1109. 航班预订统计