Given the head pointers of two linked lists, add them and return the new linked list. Each linked list represents an integer number (each node is a digit).
Question
Given the head pointers of two linked lists, add them and return the new linked list. Each linked list represents an integer number (each node is a digit).
Solution
Here is a Python solution for the problem:
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
def addTwoLists(first, second):
prev = temp = None
carry = 0
while(first is not None or second is not None):
fdata = 0 if first is None else first.data
sdata = 0 if second is None else second.data
Sum = carry + fdata + sdata
carry = 1 if Sum >= 10 else 0
Sum = Sum if Sum < 10 else Sum % 10
temp = Node(Sum)
if head is None:
head = temp
else :
prev.next = temp
prev = temp
if first is not None:
first = first.next
if second is not None:
second = second.next
if carry > 0:
temp.next = Node(carry)
return head
This Python function addTwoLists takes two linked lists first and second as input. It creates a new linked list where each node is the sum of the corresponding nodes in the input lists. If the sum is greater than 10, it carries the value to the next node.
The Node class is a simple linked list node class with a data field for storing the node's value and a next field for pointing to the next node in the list.
The addTwoLists function iterates over the two input lists simultaneously. For each pair of nodes, it adds their values together (along with any carry value from the previous node), then creates a new node with the sum. If the sum is 10 or more, it sets the carry value to 1 and reduces the sum by 10. The new node is then added to the output list.
If there is a carry value left over after all nodes have been processed, a final node is added to the output list with this carry value.
Finally, the function returns the head of the new linked list.
Similar Questions
You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty.Function DescriptionComplete the insertNodeAtTail function in the editor below.insertNodeAtTail has the following parameters:SinglyLinkedListNode pointer head: a reference to the head of a listint data: the data value for the node to insertReturnsSinglyLinkedListNode pointer: reference to the head of the modified linked listInput FormatThe first line contains an integer , the number of elements in the linked list.The next lines contain an integer each, the value that needs to be inserted at tail.ConstraintsSample InputSTDIN Function ----- -------- 5 size of linked list n = 5 141 linked list data values 141..474 302 164 530 474Sample Output141302164530474ExplanationFirst the linked list is NULL. After inserting 141, the list is 141 -> NULL.After inserting 302, the list is 141 -> 302 -> NULL.After inserting 164, the list is 141 -> 302 -> 164 -> NULL.After inserting 530, the list is 141 -> 302 -> 164 -> 530 -> NULL. After inserting 474, the list is 141 -> 302 -> 164 -> 530 -> 474 -> NULL, which is the final list.
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.You may assume the two numbers do not contain any leading zero, except the number 0 itsel
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its attribute, insert this node at the desired position and return the head node.A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is empty.Example refers to the first node in the list Insert a node at position with . The new list is Function Description Complete the function insertNodeAtPosition in the editor below. It must return a reference to the head node of your finished list.insertNodeAtPosition has the following parameters:head: a SinglyLinkedListNode pointer to the head of the listdata: an integer value to insert as data in your new nodeposition: an integer position to insert the new node, zero based indexingReturnsSinglyLinkedListNode pointer: a reference to the head of the revised listInput FormatThe first line contains an integer , the number of elements in the linked list.Each of the next lines contains an integer SinglyLinkedListNode[i].data.The next line contains an integer , the data of the node that is to be inserted.The last line contains an integer .Constraints, where is the element of the linked list..Sample Input31613712Sample Output16 13 1 7ExplanationThe initial linked list is . Insert at the position which currently has in it. The updated linked list is .
You are given the heads of two sorted linked lists list1 and list2.Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.Return the head of the merged linked list. Example 1:Input: list1 = [1,2,4], list2 = [1,3,4]Output: [1,1,2,3,4,4]Example 2:Input: list1 = [], list2 = []Output: []Example 3:Input: list1 = [], list2 = [0]Output: [0] Constraints:The number of nodes in both lists is in the range [0, 50].-100 <= Node.val <= 100Both list1 and list2 are sorted in non-decreasing order.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.