当前位置:网站首页>(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;
}
边栏推荐
- JDBC requset corresponding content and function introduction
- CS-证书指纹修改
- On weak network test of special test
- Cobalt Strike特征修改
- LeetCode 739. Daily temperature
- 模拟卷Leetcode【普通】1414. 和为 K 的最少斐波那契数字数目
- How to translate biomedical instructions in English
- How to convert flv file to MP4 file? A simple solution
- LeetCode 1200. Minimum absolute difference
- Cobalt strike feature modification
猜你喜欢
Advanced MySQL: Basics (1-4 Lectures)
E-book CHM online CS
[Tera term] black cat takes you to learn TTL script -- serial port automation skill in embedded development
金融德语翻译,北京专业的翻译公司
What are the characteristics of trademark translation and how to translate it?
今日夏至 Today‘s summer solstice
LeetCode 732. My schedule III
Selenium source code read through · 9 | desiredcapabilities class analysis
[mqtt from getting started to improving series | 01] quickly build an mqtt test environment from 0 to 1
[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
随机推荐
Biomedical English contract translation, characteristics of Vocabulary Translation
On weak network test of special test
keil MDK中删除添加到watch1中的变量
Advanced MySQL: Basics (1-4 Lectures)
Simulation volume leetcode [general] 1109 Flight reservation statistics
Career advancement Guide: recommended books for people in big factories
我的创作纪念日
Apple has open source, but what about it?
模拟卷Leetcode【普通】1061. 按字典序排列最小的等效字符串
Construction and integration of Zipkin and sleuth for call chain monitoring
利用快捷方式-LNK-上线CS
翻译公司证件盖章的价格是多少
SourceInsight Chinese garbled
Simulation volume leetcode [general] 1414 The minimum number of Fibonacci numbers with a sum of K
Py06 字典 映射 字典嵌套 键不存在测试 键排序
Selenium source code read through · 9 | desiredcapabilities class analysis
Is the test cycle compressed? Teach you 9 ways to deal with it
模拟卷Leetcode【普通】1414. 和为 K 的最少斐波那契数字数目
端午节快乐Wish Dragon Boat Festival is happy
Defense (greed), FBI tree (binary tree)