problemscpp
A collection of my answers to algorithm problems in c++.
| 函数
acwing::acwing3757 命名空间参考

struct  ListNode
 

函数

void rearrangedList (struct ListNode *head)
 
void reverse (struct ListNode *head)
 
 TEST (acwing3757, case1)
 
 TEST (acwing3757, case2)
 

函数说明

◆ rearrangedList()

void acwing::acwing3757::rearrangedList ( struct ListNode head)

在文件 acwing408.cpp125 行定义.

125 {
126 if(head == NULL || head->next == NULL)
127 return;
128 int len = 0;
129 for(struct ListNode *p = head; p != NULL; p = p->next)
130 len++;
131 int mid = (len + 1) / 2;
132 struct ListNode *a = head;
133 for(int i = 0; i < mid - 1; i++) {
134 a = a->next;
135 }
136 struct ListNode *b = a->next;
137 struct ListNode *c = b->next;
138 a->next = NULL;
139 b->next = NULL;
140 while(c) {
141 struct ListNode *tmp = c->next;
142 c->next = b;
143 b = c;
144 c = tmp;
145 }
146 struct ListNode *p = head;
147 struct ListNode *q = b;
148 while(q) {
149 auto qq = q->next;
150 // 插入结点
151 q->next = p->next;
152 p->next = q;
153 // 移动p和q
154 p = q->next;
155 q = qq;
156 }
157 }
ListNode * next
Definition: acwing.h:17
struct ListNode * next
Definition: acwing408.h:52

引用了 acwing::acwing3757::ListNode::next.

被这些函数引用 TEST().

◆ reverse()

void acwing::acwing3757::reverse ( struct ListNode head)

◆ TEST() [1/2]

acwing::acwing3757::TEST ( acwing3757  ,
case1   
)

在文件 acwing408_test.cpp501 行定义.

501 {
502 auto head = new ListNode{1,
503 new ListNode{2,
504 new ListNode{3,
505 new ListNode{4, nullptr}}}};
506 rearrangedList(head);
507 ASSERT_EQ(1, head->val);
508 ASSERT_EQ(4, head->next->val);
509 ASSERT_EQ(2, head->next->next->val);
510 ASSERT_EQ(3, head->next->next->next->val);
511 delete head->next->next->next;
512 delete head->next->next;
513 delete head->next;
514 delete head;
515 }
void rearrangedList(struct ListNode *head)
Definition: acwing408.cpp:125

引用了 rearrangedList().

◆ TEST() [2/2]

acwing::acwing3757::TEST ( acwing3757  ,
case2   
)

在文件 acwing408_test.cpp517 行定义.

517 {
518 auto head = new ListNode{1,
519 new ListNode{2,
520 new ListNode{3,
521 new ListNode{4,
522 new ListNode{5, nullptr}}}}};
523 rearrangedList(head);
524 ASSERT_EQ(1, head->val);
525 ASSERT_EQ(5, head->next->val);
526 ASSERT_EQ(2, head->next->next->val);
527 ASSERT_EQ(4, head->next->next->next->val);
528 ASSERT_EQ(3, head->next->next->next->next->val);
529 delete head->next->next->next->next;
530 delete head->next->next->next;
531 delete head->next->next;
532 delete head->next;
533 delete head;
534 }

引用了 rearrangedList().