Question

Write a function using c that combines two lists by alternatingly taking elements from the two...

Write a function using c that combines two lists by alternatingly taking elements from the two lists that are passed to it. e.g. [a,b,c], [1,2,3] → [a,1,b,2,c,3].

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Function to combines two lists alternatively

struct Node *combinelist(struct Node *a,struct Node *b)
{
if(!a)
return b;
  
if(!b)
return a;

struct Node *head=a,*res=a,*prev=NULL;
while(a && b)
{
struct Node *t=a->next;
   struct Node *q=b->next;
res->next=b;
res=res->next;
res->next=t;
prev=res;
res=res->next;
a=t;
b=q;
}
if(a)
prev->next=a;
else if(b)
prev->next=b;

return head;
}

//If you have any queries,please ask in the comment section

//If you got the answer,please upvote:)

Add a comment
Know the answer?
Add Answer to:
Write a function using c that combines two lists by alternatingly taking elements from the two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns...

    ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of...

  • Function Name: zip_d Parameters: two equal-sized lists Returns: a dictionary Description: Write a function, zip_d, that...

    Function Name: zip_d Parameters: two equal-sized lists Returns: a dictionary Description: Write a function, zip_d, that takes a two equal-sized lists as parameters, and zips each list into a dictionary. Sample Inputs/Outputs: Example Call: zip_d([1,2,3], ['a','b','c']) Expected Return: {1: 'a', 2: 'b', 3: 'c'} Example Call: zip_d([‘a’,’b’,’c’],[9.8,7.6,5.4]) Expected Return: {‘a’:9.8, ’b’:7.6, ’c’:5.4} please answer in python

  • Python. Write a function swap_lists that takes in two lists and swap their elements in place....

    Python. Write a function swap_lists that takes in two lists and swap their elements in place. You can not use another list. You can not return lists. Input lists have the same size. def swap_lists(alist1, alist2): """Swaps content of two lists. Does not return anything. >>> list1 = [1, 2] >>> list2 = [3, 4] >>> swap_lists(list1, list2) >>> print(list1) [3, 4] >>> print(list2) [1, 2] >>> list1 = [4, 2, 6, 8, 90, 45] >>> list2 = [30, 41,...

  • Write a c program that finds the uncommon elements from two array elements using pointers only...

    Write a c program that finds the uncommon elements from two array elements using pointers only . For example : Enter the length of the array one : 5 Enter the elements of the array one: 9 8 5 6 3 Enter the length of the array two: 4 Enter the elements of the array two: 6 9 2 1 output: 8 5 3 2 1 void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size); The function...

  • Make a function dict build(keys, values) which takes as argument two lists, one containing keys, one...

    Make a function dict build(keys, values) which takes as argument two lists, one containing keys, one values to be put into a dictionary (in order). If the lists are of the same length put them into a dictionary form, and return the dictionary from the function. Otherwise return the keyword None from the function. For example: Test print(di ct_build(['a', 'b' , 'c'], [1,2,3])={ 'a': 1, 'b': 2, 'c': 3 r example: Result ildCC'a', 'b', 'c'], [1,2,3])-= { 'a': 1, 'b':...

  • Write a program in ADA that should be able to read two lists and produce as...

    Write a program in ADA that should be able to read two lists and produce as output the shuffle of two lists (all shuffledlists): You type [a,b,c] [1,2,3] and as output you will obtain [a,b,c,1,2,3]; [a,b,1,c,2,3]; .... [1,2,3,a,b,c] The representation of list would be your choice (arrays or linked lists)

  • Write a function that takes two lists that are in ascending order as arguments. The function...

    Write a function that takes two lists that are in ascending order as arguments. The function should return a new list that has all the values from the two argument lists and is also in ascending order (e.g., [1,3,5] and [2,4,6] become [1,2,3,4,5,6]). Please use python

  • USING C++: Write a function that will return the number of elements that appear in pairs...

    USING C++: Write a function that will return the number of elements that appear in pairs in a vector (i.e., the number of elements that appear exactly twice). E.g. if the vector v={1,3,7,10,1,1,3,5,10,5}, the function will return 3 (since 3, 5 and 10 appear in pairs) The function prototype is given below: template <class T> int pairCount(const vector<T>&);

  • Using C++ Write a function that given two dynamical arrays (A and B) of doubles of...

    Using C++ Write a function that given two dynamical arrays (A and B) of doubles of a given size N, will return a N by N dynamical array C such that C[i][j]=A[i]*B[j]. e.g. A={1,2,3}, B={10,20,30}, will result in C={{10,20,30},{20,40,60}, {30, 60, 90}} double** outerProd(double *A, double *B, int size);

  • 2. Given two unsorted STL lists X and P, write a valid C++ function intersection(X, P)...

    2. Given two unsorted STL lists X and P, write a valid C++ function intersection(X, P) that returns a new list that contains the elements common to both X and P. For example, if X = 5, 2, 1, 4 and P = 4, 5, 7; your algorithm should return a new list containing 5 and 4 (order is not important). Also specify the running time of your algorithm using Big-Oh notation. Hint: As the lists are unsorted, a straightforward...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT