Hello I was wondering why the time complexity of this function is O(n) even though there is a nested loop:// This function convert a 2-d matrix into a 1-D linear list
Node* flatten(Node* head) {
if (head == nullptr) {
return head;
}
Node* curr = head;
while (curr != nullptr) {
// If there's a next row, splice it in.
if (curr->nextRow != nullptr) {
Node* nextColNode = curr->next;
curr->next = curr->nextRow;
curr->nextRow->prev = curr;
Node* rowTail = curr->nextRow;
// Find the tail of the next row.
while (rowTail->next != nullptr) {
rowTail = rowTail->next;
}
// Connect tail to the next column node.
rowTail->next = nextColNode;
if (nextColNode != nullptr) {
nextColNode->prev = rowTail;
}
curr->nextRow = nullptr;
}
curr = curr->next;
}
return head;
}