""" 0203.1 - Remove Linked List Elements - Solution 1 - Dummy Node Iteration """
#####################################################################################
# Imports
#####################################################################################
from typing import Optional
#####################################################################################
# Classes
#####################################################################################
class ListNode:
"""ListNode Class"""
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
"""Solution Class"""
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
"""Remove Linked List Elements Function"""
dummy = ListNode(0, head)
curr = dummy
while curr.next:
if curr.next.val == val:
curr.next = curr.next.next
else:
curr = curr.next
return dummy.next
#####################################################################################
# Functions
#####################################################################################
def build_list(values):
dummy = ListNode(0)
cur = dummy
for v in values:
cur.next = ListNode(v)
cur = cur.next
return dummy.next
def to_list(head):
out = []
while head:
out.append(head.val)
head = head.next
return out
def testcase():
"""Test Function"""
s = Solution()
print(to_list(s.removeElements(build_list([1, 2, 6, 3, 4, 5, 6]), 6))) # [1, 2, 3, 4, 5]
print(to_list(s.removeElements(build_list([]), 1))) # []
print(to_list(s.removeElements(build_list([7, 7, 7, 7]), 7))) # []
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
/** 0203.1 - Remove Linked List Elements - Solution 1 - Dummy Node Iteration */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class ListNode {
constructor(val = 0, next = null) {
this.val = val;
this.next = next;
}
}
class Solution {
removeElements(head, val) {
const dummy = new ListNode(0, head);
let cur = dummy;
while (cur.next) {
if (cur.next.val === val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return dummy.next;
}
}
/** 0203.1 - Remove Linked List Elements - Solution 1 - Dummy Node Iteration */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class ListNode {
constructor(public val: number = 0, public next: ListNode | null = null) {}
}
class Solution {
removeElements(head: ListNode | null, val: number): ListNode | null {
const dummy = new ListNode(0, head);
let cur: ListNode = dummy;
while (cur.next) {
if (cur.next.val === val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return dummy.next;
}
}
// 0203.1 - Remove Linked List Elements - Solution 1 - Dummy Node Iteration
package main
/////////////////////////////////////////////////////////////////////////////////////
// Structs
/////////////////////////////////////////////////////////////////////////////////////
type ListNode struct {
Val int
Next *ListNode
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
func removeElements(head *ListNode, val int) *ListNode {
dummy := &ListNode{Next: head}
cur := dummy
for cur.Next != nil {
if cur.Next.Val == val {
cur.Next = cur.Next.Next
} else {
cur = cur.Next
}
}
return dummy.Next
}
func main() {}
""" 0203.2 - Remove Linked List Elements - Solution 2 - Recursive """
#####################################################################################
# Imports
#####################################################################################
from typing import Optional
#####################################################################################
# Classes
#####################################################################################
class ListNode:
"""ListNode Class"""
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
"""Solution Class"""
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
"""Remove Linked List Elements Function"""
if not head:
return None
head.next = self.removeElements(head.next, val)
return head.next if head.val == val else head
#####################################################################################
# Functions
#####################################################################################
def build_list(values):
dummy = ListNode(0)
cur = dummy
for v in values:
cur.next = ListNode(v)
cur = cur.next
return dummy.next
def to_list(head):
out = []
while head:
out.append(head.val)
head = head.next
return out
def testcase():
"""Test Function"""
s = Solution()
print(to_list(s.removeElements(build_list([1, 2, 6, 3, 4, 5, 6]), 6))) # [1, 2, 3, 4, 5]
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
/** 0203.2 - Remove Linked List Elements - Solution 2 - Recursive */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class ListNode {
constructor(val = 0, next = null) {
this.val = val;
this.next = next;
}
}
class Solution {
removeElements(head, val) {
if (!head) return null;
head.next = this.removeElements(head.next, val);
return head.val === val ? head.next : head;
}
}
/** 0203.2 - Remove Linked List Elements - Solution 2 - Recursive */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class ListNode {
constructor(public val: number = 0, public next: ListNode | null = null) {}
}
class Solution {
removeElements(head: ListNode | null, val: number): ListNode | null {
if (!head) return null;
head.next = this.removeElements(head.next, val);
return head.val === val ? head.next : head;
}
}
// 0203.2 - Remove Linked List Elements - Solution 2 - Recursive
package main
/////////////////////////////////////////////////////////////////////////////////////
// Structs
/////////////////////////////////////////////////////////////////////////////////////
type ListNode struct {
Val int
Next *ListNode
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
func removeElementsRecursive(head *ListNode, val int) *ListNode {
if head == nil {
return nil
}
head.Next = removeElementsRecursive(head.Next, val)
if head.Val == val {
return head.Next
}
return head
}
func main() {}