EC Lecture Notes - Lecture 1: Linked List
Document Summary
L: l0 l1 ln-1 ln, reorder it to: You must do this in-place without altering the nodes" values. Given {1,2,3,4} , reorder it to {1,4,2,3} . // c++ program to rearrange a linked list in-place. // linkedlist node structure struct node { int data; struct node* next; // function to create newnode in a linkedlist. Node* temp = new node; temp->data = key; temp->next = null; return temp; // function to reverse the linked list void reverselist(node** head) Node *prev = null, *curr = *head, *next; while (curr) { next = curr->next; curr->next = prev; prev = curr; curr = next; *head = prev; reorder list1// function to print the linked list void printlist(node* head) while (head != null) { cout << head->data << " "; if (head->next) cout << "-> "; head = head->next; cout << endl; // function to rearrange a linked list void rearrange(node** head) // 1) find the middle point using tortoise and hare.