No results found for the specified position. 3 Python Intermediate Level Linked Lists Interview Questions

MockQuestions

Python Intermediate Level Linked Lists Mock Interview

To help you prepare for your Python Intermediate Level Linked Lists interview, here are 3 interview questions and answer examples.

Get More Information About Our Python Intermediate Level Linked Lists Interview Questions

Question 1 of 3

Remove the duplicates from this sorted singly linked list.

This interview question concentrates on using Python duplicates and linked lists.

You are given a sorted singly linked list. You need to remove the duplicates from the linked list and return their head node. The returned list must also be a sorted list.

/*Example*/

Given list- 1 -> 1 -> 2
expected output- 1 -> 2

Given list- 1 -> 2 -> 2 -> 3 -> 4 -> 4 -> 4
expected output- 1 -> 2 -> 3 -> 4

Solution:

We will traverse the whole list. If we find a duplicate, we will delete it.
Let curr be the pointer to the current at which we are. It will point to the first occurrence of a number.
Let nextNode be the next node of curr.

We will compare the value of curr with the value of nextNode-
-> if the values are not equal, it means we don’t have any more duplicate corresponding to the value of curr, we will update curr to point to the next node.
-> if the values are equal, we will delete nextNode. We will delete nextNode by pointing next of curr to next of nextNode. So in the illustration below, curr is at 41 and nextNode is at 42. Their values are equal. So we will delete 42. We will point the next pointer of 41 to the next of 42 i.e. 43.

(The subscript shows the identity of nodes corresponding to the same value)

nextNode
|
0 -> 1 -> 2 -> 3 -> 41 -> 42 -> 43 -> 44 -> 5
| |
curr nextNode.next

After deleting nextNode (42)-
nextNode
|
0 -> 1 -> 2 -> 3 -> 41 -> 43 -> 44 -> 5
| |
curr nextNode.next

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

def findMiddleElement(head):
	curr = newLL = ListNode(None)

	while head:
		if curr.val == head.val:
			head = head.next
			continue
		
		curr.next = ListNode(head.val)

		# move both pointers
		curr = curr.next
		head = head.next

	return newLL.next

Time complexity - O(n)
Space complexity - O(1)

Written by on May 22nd, 2021

Next Question

3 Python Intermediate Level Linked Lists Interview Questions & Answers

Below is a list of our Python Intermediate Level Linked Lists interview questions. Click on any interview question to view our answer advice and answer examples. You may view 5 answer examples before our paywall loads. Afterwards, you'll be asked to upgrade to view the rest of our answers.

  • 1. Remove the duplicates from this sorted singly linked list.

  • 2. Given the head of a singly linked list, return the reversed list.

  • 3. Where do the two linked lists intersect?