13.07.2015 Views

Linked List Problems

Linked List Problems

Linked List Problems

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

31while (a!=NULL && b!=NULL) {if (a->data == b->data) {Push((&tail->next), a->data);tail = tail->next;a = a->next;b = b->next;}else if (a->data < b->data) { // advance the smaller lista = a->next;}else {b = b->next;}}}return(dummy.next);// This solution uses the local referencestruct node* SortedIntersect2(struct node* a, struct node* b) {struct node* result = NULL;struct node** lastPtrRef = &result;// Advance comparing the first nodes in both lists.// When one or the other list runs out, we're done.while (a!=NULL && b!=NULL) {}if (a->data == b->data) {Push(lastPtrRef, a->data);lastPtrRef = &((*lastPtrRef)->next);a=a->next;b=b->next;// found a node for the intersection}else if (a->data < b->data) { // advance the smaller lista=a->next;}else {b=b->next;}}return(result);17 — Reverse() SolutionThis uses the back-middle-front strategy described in the problem statement. We onlymake one pass of the list.// Reverses the given linked list by changing its .next pointers and// its head pointer. Takes a pointer (reference) to the head pointer.void Reverse(struct node** headRef) {if (*headRef != NULL) {// special case: skip the empty list/*Plan for this loop: move three pointers: front, middle, backdown the list in order. Middle is the main pointer runningdown the list. Front leads it and Back trails it.For each step, reverse the middle pointer and then advance all

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!