Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include struct Node { int val; struct Node *next; }; void displayList(struct Node *head); struct Node * destroyList(struct Node *head); struct Node * createList(int

image text in transcribed

#include #include

struct Node { int val; struct Node *next; };

void displayList(struct Node *head); struct Node * destroyList(struct Node *head); struct Node * createList(int startVal, int finishVal);

struct Node *reorderList(struct Node *head);

void main() {

struct Node *p = createList(3,13);

p = createList(3,13); printf(" Created a list: "); displayList(p); p =reorderList(p); printf("Reordered list: "); displayList(p); p = destroyList(p);

p = createList(3,12); printf(" Created a list: "); displayList(p); p =reorderList(p); printf("Reordered list: "); displayList(p); p = destroyList(p);

}

struct Node *reorderList(struct Node *head) { return head; }

void displayList(struct Node *head) { printf("List: "); for (struct Node *p=head; p!=NULL; p=p->next) { printf("->%d ",p->val); } printf(" "); }

struct Node * destroyList(struct Node *head) { struct Node *p = head; while (p!=NULL) { struct Node *next = p->next; free(p); p = next; } return NULL; }

struct Node *createList(int startVal, int finishVal) { struct Node *head = NULL; /* Head of the list */ struct Node *last = NULL; /* Last node in the list */ for (int i=startVal; ival = i; p->next = NULL; if (i == startVal) { head = p; } else { last->next = p; } last = p; } return head; }

Problem 3 [ 2 pts]. Attached with this homework is a file "reorder.c" that has the following function: struct Node * reorderList(struct Node * head); Reorders the linked list as follows and returns its head Input: L(0)>L(1)>L(2)>L(n1)>NULL Output: L(0)>L(n1)>L(1)>L(n2)>L(2)>L(n3)NULL Implement this function correctly and use O(n) time complexity and O(1) space complexity (amount of addition memory used by the function). You may not modify the data values (int val) in the nodes, only the pointer valu ('next') may be changed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Eric Redmond ,Jim Wilson

1st Edition

1934356921, 978-1934356920

More Books

Students also viewed these Databases questions

Question

Describe the factors influencing of performance appraisal.

Answered: 1 week ago

Question

What is quality of work life ?

Answered: 1 week ago

Question

1. Why do people tell lies on their CVs?

Answered: 1 week ago

Question

2. What is the difference between an embellishment and a lie?

Answered: 1 week ago