当前位置:网站首页>(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;
}
边栏推荐
- 国际经贸合同翻译 中译英怎样效果好
- How to convert flv file to MP4 file? A simple solution
- 中英对照:You can do this. Best of luck祝你好运
- 关于新冠疫情,常用的英文单词、语句有哪些?
- Address bar parameter transmission of list page based on jeecg-boot
- The internationalization of domestic games is inseparable from professional translation companies
- MySQL is sorted alphabetically
- org. activiti. bpmn. exceptions. XMLException: cvc-complex-type. 2.4. a: Invalid content beginning with element 'outgoing' was found
- 模拟卷Leetcode【普通】1219. 黄金矿工
- LeetCode 729. My schedule I
猜你喜欢
What are the commonly used English words and sentences about COVID-19?
翻译影视剧字幕,这些特点务必要了解
Traffic encryption of red blue confrontation (OpenSSL encrypted transmission, MSF traffic encryption, CS modifying profile for traffic encryption)
keil MDK中删除添加到watch1中的变量
sourceInsight中文乱码
Changes in the number of words in English papers translated into Chinese
On weak network test of special test
LeetCode 1200. Minimum absolute difference
国产游戏国际化离不开专业的翻译公司
专业论文翻译,英文摘要如何写比较好
随机推荐
Changes in the number of words in English papers translated into Chinese
模拟卷Leetcode【普通】1405. 最长快乐字符串
How do programmers remember code and programming language?
Black cat takes you to learn EMMC Protocol Part 10: EMMC read and write operation details (read & write)
A 27-year-old without a diploma, wants to work hard on self-study programming, and has the opportunity to become a programmer?
金融德语翻译,北京专业的翻译公司
Simulation volume leetcode [general] 1218 Longest definite difference subsequence
Distributed system basic (V) protocol (I)
模拟卷Leetcode【普通】1249. 移除无效的括号
Making interactive page of "left tree and right table" based on jeecg-boot
模拟卷Leetcode【普通】1219. 黄金矿工
MySQL5.72. MSI installation failed
字幕翻译中翻英一分钟多少钱?
Simulation volume leetcode [general] 1061 Arrange the smallest equivalent strings in dictionary order
E-book CHM online CS
QT: the program input point xxxxx cannot be located in the dynamic link library.
Past and present lives of QR code and sorting out six test points
Lecture 8: 1602 LCD (Guo Tianxiang)
模拟卷Leetcode【普通】1091. 二进制矩阵中的最短路径
How effective is the Chinese-English translation of international economic and trade contracts