Enhance your problem-solving skills by solving the Remove Linked List Elements problem on LeetCode and documenting your solution approach.
Solve the LeetCode problem: https://leetcode.com/problems/remove-linked-list-elements/description/Links to an external site.
Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
Input: head = [1,2,6,3,4,5,6], val = 6 Output: [1,2,3,4,5]
Input: head = [], val = 1 Output: []
Input: head = [7,7,7,7], val = 7 Output: []
The number of nodes in the list is in the range [0, 104]. 1 <= Node.val <= 50 0 <= val <= 50
traverse the linked list, start a new linked list, for each step check to see if the data matches the passed value, if it does, it is not added to new linked list. setup linked list with a fake head to initialize, then pass it values that do not equal the target.
return the head of the new linked list as requirement, with some logic to filter empty lists and handle empty list results.
Only using one while loop so time complexity is O(n) and since it is a linked list, updating pointers and not values it has a space complexity of O(1) which is ideal in both cases as it is neccessary to traverse the entire list to search for target values.
Using boilerplate from class for initialization and traversal, modified logic to remove selected elements. This challenge is similar to a problem I solved and I accidentaly created this code expecting it to select the values between targets, but my code didnt work. I saved it with notes, and it was reused for this assignment. I worked offline in vsc.
Python 3
see attached screenshots
document is readme file
my choices were made to comply with dynamic programming best practices
only one while loop used to minimize time complexity
class ListNode(object): def init(self, val=0, next=None): self.val = val self.next = next
class Solution(object): def removeElements(self, head, val): fake_head = ListNode(0) fake_head.next = head current = fake_head
while current.next is not None:
if current.next.val == val:
current.next = current.next.next
else:
current = current.next
return fake_head.next
def create_linked_list(self, lst):
if not lst:
return None
head = ListNode(lst[0])
current = head
for value in lst[1:]:
current.next = ListNode(value)
current = current.next
return head
space complexity O(1) time comlexity O(n)
I was able to quickly solve this as code I wrote in the past solved this case. I originnally was writing to solve the selection betweentarget values and it selected everything but the target element. I saved the useful mistake documenting the problem and behavior.
it took minimal updating for this application which I did in VSC.
the git repo includes this readme with my submission details.