题干
![]()
思路
![]()
题目答案
手写版(略微修改)
若无法显示,请点击https://telegraph-image.pages.dev/file/49854306da8b17e3bc502.png
![]()
C代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| struct ListNode { int val; struct ListNode *next; };
void reverseLinkedList(struct ListNode *head) { struct ListNode *prev = NULL; struct ListNode *curr = head;
while (curr != NULL) { struct ListNode *next = curr->next; curr->next = prev; prev = curr; curr = next; } }
void reverseBetween(struct ListNode *head, int left, int right) { struct ListNode *p1 = head; for (int i = 0; i < left - 1; i++) { p1 = p1->next; } struct ListNode *p2 = p1->next; struct ListNode *p3 = p2; for (int i = 0; i < right - left; i++) { p3 = p3->next; } struct ListNode *p4 = p3->next;
p1->next = NULL; p3->next = NULL;
reverseLinkedList(p2);
p1->next = p3; p2->next = p4; }
|
题目来源
千葉原