answersLogoWhite

0

Concatenate two singly linked lists

Updated: 8/10/2023
User Avatar

Wiki User

12y ago

Best Answer

If you mean how to add all the elements of one list to another, then the answer is simple: link the last element of one to the first element of the other and you will have one large linked list.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago
  1. We can do this as follows:

    1. traverse first list

    2. while traversing it self calculate the number value by num1 = (num1*10) + value at node of list1.

    3. similarly num2 = (num2*10) + value at node of list2

    4. Sum = num1 + num2

  2. //WAP to SumTwo Number Which are represented by node in linked list #include #include #include /* Link list node */ struct node { int data; struct node* next; }; /* Function to reverse the linked list */ static void reverse(struct node** head_ref) { struct node* prev = NULL; struct node* current = *head_ref; struct node* next; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } *head_ref = prev; } /* Function to push a node */ void push(struct node** head_ref, int new_data) { /* allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); /* put in the data */ new_node->data = new_data; /* link the old list off the new node */ new_node->next = (*head_ref); /* move the head to point to the new node */ (*head_ref) = new_node; } struct node* addition (struct node* temp1, struct node* temp2) { struct node* prev = NULL; int carry = 0,a,b,result; while (temp1 temp2) //while both lists exist { if(temp1) a=temp1->data; else a=0; if(temp2) b=temp2->data; else b=0; result = carry; if(temp1) result+=temp1->data; if(temp2) result+=temp2->data; carry=result/10; struct node * newnode = (struct node*) malloc (sizeof(struct node)); newnode->data=(result)%10; newnode->next = prev; prev=newnode; if(temp1) temp1=temp1->next; if(temp2) temp2=temp2->next; } return prev; } void printList(struct node *node) { while(node != NULL) { printf("%d ", node->data); node = node->next; } } /* Drier program to test above function*/ int main(void) { /* Start with the empty list */ struct node* head = NULL; struct node* head1 = NULL; struct node* head2 = NULL; /* Created Linked list is 1->2->3->4->5->6->7->8 */ push(&head1, 4); push(&head1, 3); push(&head1, 2); push(&head1, 1); printf("list 1 is \t"); printList(head1); printf("\n"); reverse(&head1); push(&head2, 6); push(&head2, 5); push(&head2, 4); printf("list 2 is \t"); printList(head2); printf("\n"); reverse(&head2); head=addition(head1,head2); printf("resultant list is \t"); printList(head); printf("\n"); return(0); }
This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Create two different singly linked lists.

make the head ( or starting pointer) of the 2nd singly linked list, the link of the last (or end) node of the 1st linked list.

e.g. last->link=first;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Concatenate two singly linked lists
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

A Write the algorithm to concatenate two given strings?

a write the algorithm to concatenate two given string


Traversing in Doubly Linked List is faster then Singly Linked List?

Traversing a doubly linked list is generally faster than traversing a singly linked list, but the speedup depends on how you do the traversal:Traversing from first to last node: No difference.Random access: Doubly linked list is faster, the difference is a fixed factor. (Like twice as fast. Which makes it still very slow for random access compared to arrays.)Reverse order: Doubly linked list is just as fast traversing "backwards" as "forwards", while a singly linked list traversing in reverse order needs to traverse the entire list once for every element in the list - it is a LOT slower. (Time complexity O(n) for doubly linked list, O(n*n) for singly linked, if you are familiar with the notation.)If you are talking about the computer science "big O notation", doubly linked and singly liked lists are the same. This is because the Big O notation ignores fixed factors and only looks at how time increases with the length of the list, and in this respect the two are the same. (Except for the special case of traversing the list in reverse order. Even here a singly linked list could do it in O(n) time - same as a doubly linked list - by reversing the list (O(n)) before traversing it (O(n)) for a total time of 2*O(n), which by the rules of Big O is the same as O(n).)


What is difference between linked list and singly linked list?

Answersingly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer.but in a doubly linked list, the node pointer points to the both previous and the next node.singly linked list has two nodesdoubly linked list has three nodesA doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.


What is concatenate?

Concatenate in MS Excel and most of other tool is function to join text.


What are the basic types of link list in java?

linear circular double linked linear double linked circular Knowing the names does not help much when your teacher will require you to actually know what the names mean. Start reading. Programming requires lots of reading.

Related questions

What the different between single and double linked list regarding space and operation?

Doubly linked lists require more memory than singly linked lists because each node in a doubly-linked list requires two pointers whereas each node in a singly-linked list only requires one pointer. In terms of operation, doubly-linked lists are only useful if you need bi-directional traversal of the the list. If you only need mono-directional traversal, a singly-linked list is more efficient. However, linked lists of either sort do not perform well when random access is essential. In this case a vector or an array will provide constant time access to any element, and memory consumption is further reduced since there is no longer a need for pointers. However, dynamic expansion of an array can be costly in terms of memory consumption and performance. In cases where random access and scalability are required, one or the other must be compromised.


How to write aC program to merge two singly linked list?

write a c program to circular queue


A Write the algorithm to concatenate two given strings?

a write the algorithm to concatenate two given string


What is a singly linked linear list?

A singly linked list is a linked list which only provides links in "one direction". Using a metaphor, a singly linked list is a one way street, while a doubly linked list is a two way street. Once you move forward in a singly linked list, there is no way to go backwards unless you kept your reference/pointer from before. A singly linked list would look like this: start ----> node1---->node2---->node3 ----> NULL You will see that node2 only has a link forward to node3 - it does not have a link backwards to node1, even though node1 has a link forwards to node2. To prevent us from permanently losing access to portions of the linked list, we generally keep a reference/pointer to "start". A doubly linked list would have twice the number of pointers/references as a singly linked list - making it very inefficient to store small datatypes. On the other hand, it would be possible to move both forwards and backwards with a doubly linked list because you have links pointing both forwards and backwards.


How do you implement a doubly linked list by using singly linked list?

To implement a doubly linked list using a singly linked list, you can create two nodes in each element of the singly linked list - one for the next element and another for the previous element. This way, each node will have access to both its previous and next nodes, effectively creating a doubly linked list structure using a singly linked list implementation.


Traversing in Doubly Linked List is faster then Singly Linked List?

Traversing a doubly linked list is generally faster than traversing a singly linked list, but the speedup depends on how you do the traversal:Traversing from first to last node: No difference.Random access: Doubly linked list is faster, the difference is a fixed factor. (Like twice as fast. Which makes it still very slow for random access compared to arrays.)Reverse order: Doubly linked list is just as fast traversing "backwards" as "forwards", while a singly linked list traversing in reverse order needs to traverse the entire list once for every element in the list - it is a LOT slower. (Time complexity O(n) for doubly linked list, O(n*n) for singly linked, if you are familiar with the notation.)If you are talking about the computer science "big O notation", doubly linked and singly liked lists are the same. This is because the Big O notation ignores fixed factors and only looks at how time increases with the length of the list, and in this respect the two are the same. (Except for the special case of traversing the list in reverse order. Even here a singly linked list could do it in O(n) time - same as a doubly linked list - by reversing the list (O(n)) before traversing it (O(n)) for a total time of 2*O(n), which by the rules of Big O is the same as O(n).)


Is there a c program to multiply two polynomials using linked lists?

yes


What is difference between linked list and singly linked list?

Answersingly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer.but in a doubly linked list, the node pointer points to the both previous and the next node.singly linked list has two nodesdoubly linked list has three nodesA doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.


What is concatenate?

Concatenate in MS Excel and most of other tool is function to join text.


What are the basic types of link list in java?

linear circular double linked linear double linked circular Knowing the names does not help much when your teacher will require you to actually know what the names mean. Start reading. Programming requires lots of reading.


How do you concatenate a string?

v can concatenate two string by using a function like: select CONCAT( CONCAT('ename','e_mrks'),"name","marks" from student;


What to do with two peaces of normal string?

Whatever you want. Concatenate them, for example.