Question
  1. (20 points) ) Write a Python program which merges two sorted singly linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:




Example: Input: 1->2- >4, 1->3->4 Output: 1->1->2->3 ->4->4
0 0
Add a comment Improve this question Transcribed image text
Answer #1

answer :

class ListNode:
def _init_(self, x):
self.val = x
self.next = None


class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
# Result ListNode
res = None

# Taking temporary lists
temp1 = l1
temp2 = l2

# Iterating over list 1
while True:
# If either of the list is empty
if temp1 == None or temp2 == None:
break
else:
# Comparing values
if temp2.val > temp1.val:
# Creating a temporary node
t1 = ListNode(temp1.val)
# Adding as first node
if res == None:
res = t1
else:
# Moving to last node
t = res
while t.next != None:
t = t.next
# Adding new node
t.next = t1
# Forwarding to next node
temp1 = temp1.next
# Comparing values
elif temp2.val < temp1.val:
# Creating a temporary node
t1 = ListNode(temp2.val)
# Adding as first node
if res == None:
res = t1
else:
# Moving to last node
t = res
while t.next != None:
t = t.next
# Adding new node
t.next = t1
# Forwarding to next node
temp2 = temp2.next
# Comparing values
else:
# Creating a temporary node
t1 = ListNode(temp1.val)
t2 = ListNode(temp2.val)
# Adding as first node
if res == None:
res = t1
res.next = t2
else:
# Moving to last node
t = res
while t.next != None:
t = t.next
# Adding new node
t.next = t1
t = t.next
t.next = t2
# Forwarding to next node
temp1 = temp1.next
temp2 = temp2.next
# Return new list
return res

def printList(l):
""" Function that prints the node """
temp = l;
# Iterating over list
while temp.next != None:
print(str(temp.val) + "->" , end="")
temp = temp.next
print(str(temp.val))

  
# Creating two Lists
n1 = ListNode(1)
n2 = ListNode(2)
n3 = ListNode(4)
n2.next = n3
n1.next = n2
l1 = n1

n21 = ListNode(1)
n22 = ListNode(3)
n23 = ListNode(4)
n22.next = n23
n21.next = n22
l2 = n21

print("\nList 1: ", end="")
printList(l1)
print("\nList 2: ", end="")
printList(l2)

sObj = Solution()
rL = sObj.mergeTwoLists(l1, l2)
print("\n\nResult List: ", end="")
printList(rL)

___________________________________

Sample Run:(out put)

C:\Users\SaiBabu\AppData\Local\Programs\Python Python35>python d:\Python\ListNode.py List 1: 1->2->4 List 2: 1->3->4 Result L

Add a comment
Know the answer?
Add Answer to:
(20 points) ) Write a Python program which merges two sorted singly linked lists and return...
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
  • Please solve using java. 21. Merge Two Sorted Lists Easy 2705 396 ♡ Favorite Sha *...

    Please solve using java. 21. Merge Two Sorted Lists Easy 2705 396 ♡ Favorite Sha * Definition for singly-linked list. * public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. class Solution { public ListNode merge TwoLists(ListNode li, ListNode 12) { 10 Example: Input: 1->2->4, 1-3-4...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • c++ Write pseudocode that merges two sorted lists into a new third sorted list by using...

    c++ Write pseudocode that merges two sorted lists into a new third sorted list by using only ADT sorted list operations.

  • Data Structure C++ Write pseudocode that merges two sorted lists into a new third sorted list...

    Data Structure C++ Write pseudocode that merges two sorted lists into a new third sorted list by using only ADT sorted list operations.

  • java singly linked list write a routine, which will travel through the list second and third...

    java singly linked list write a routine, which will travel through the list second and third list by taking every 2nd node from the first list and 3rd node Given a pointer to creating from the first list and putting them into the two newly created lists. (For example: If the original list had 12 nodes it should remain with nodes 1, 4, 7, and 10. The 2nd list would be made up of nodes 2, 5, 8, and 11...

  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

  • 2 Given a pointer to a singly linked list write a routine, which will travel through...

    2 Given a pointer to a singly linked list write a routine, which will travel through t creating a second list by taking every 3nd node from the first list and putting it ir newly created list. (For example: If the original list had 12 nodes it should remai nodes 1, 2, 4, 5, 7, 8, 10 and 11 while the 2nd list would be made up of nodes 3, 6, 12. Briefly explain: A. The advantage of implementing a...

  • 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...

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
ADVERTISEMENT