当前位置:网站首页>(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;
}
边栏推荐
- CS certificate fingerprint modification
- Simulation volume leetcode [general] 1061 Arrange the smallest equivalent strings in dictionary order
- leetcode 24. 两两交换链表中的节点
- [no app push general test plan
- Black cat takes you to learn UFS Protocol Part 8: UFS initialization (boot operation)
- Convert the array selected by El tree into an array object
- MFC关于长字符串unsigned char与CString转换及显示问题
- Use shortcut LNK online CS
- Simulation volume leetcode [general] 1062 Longest repeating substring
- Distributed system basic (V) protocol (I)
猜你喜欢
E-book CHM online CS
Biomedical localization translation services
Modify the list page on the basis of jeecg boot code generation (combined with customized components)
CS certificate fingerprint modification
Thesis abstract translation, multilingual pure human translation
Phishing & filename inversion & Office remote template
What are the commonly used English words and sentences about COVID-19?
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
电子书-CHM-上线CS
Black cat takes you to learn UFS protocol Chapter 4: detailed explanation of UFS protocol stack
随机推荐
Use shortcut LNK online CS
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
University of Manchester | dda3c: collaborative distributed deep reinforcement learning in swarm agent systems
Career advancement Guide: recommended books for people in big factories
在JEECG-boot代码生成的基础上修改list页面(结合自定义的组件)
Financial German translation, a professional translation company in Beijing
My daily learning records / learning methods
How much is it to translate Chinese into English for one minute?
Luogu p2089 roast chicken
Drug disease association prediction based on multi-scale heterogeneous network topology information and multiple attributes
In English translation of papers, how to do a good translation?
Today's summer solstice
今日夏至 Today‘s summer solstice
Delete the variables added to watch1 in keil MDK
Simulation volume leetcode [general] 1249 Remove invalid parentheses
How to do a good job in financial literature translation?
Error getting a new connection Cause: org. apache. commons. dbcp. SQLNestedException
Qt:无法定位程序输入点XXXXX于动态链接库。
Simulation volume leetcode [general] 1218 Longest definite difference subsequence
The internationalization of domestic games is inseparable from professional translation companies