Skip to main content

One Pass

One pointer

Array to Linked List

Array to Linked List

Easy1 solutionanalysis1 playground

Solutions:

Analysis

def array_to_linked_list(arr):
head = ListNode(arr[0])
curr = head
for i in range(1, len(arr)):
curr.next = ListNode(arr[i])
curr = curr.next
return head

1290. Convert Binary Number in a Linked List to Integer

Easy2 solutions2 playgrounds

Solutions:

The accumulator number appears in the variables panel — each step shows number = number * 2 + curr.val (or bit-shift in the second solution). The step's description line shows the formula at that bit; watch number grow as curr advances. The legacy view drew this on a separate formula panel.

def getDecimalValue(head):
number = 0
curr = head
while curr:
number = number * 2 + curr.val
curr = curr.next
return number

3263. Convert Doubly Linked List to Array I

Easy2 solutions1 playground

Solutions:

def toArray(head):
array = []
curr = head
while curr:
array.append(curr.val)
curr = curr.next
return array
search-in-linked-list


3294. Convert Doubly Linked List to Array II

Medium2 solutions1 playground

Solutions:

The deque grows in the variables panel — first phase prepends with appendleft (walking left via prev), then phase two appends with append (walking right via next). The legacy view used a custom widget with a direction banner; here, watch the array line in the variables panel and the phase chip.

def toArray(head):
array = collections.deque()
curr = head
while curr:
array.appendleft(curr.val)
curr = curr.prev
curr = head.next if head else None
while curr:
array.append(curr.val)
curr = curr.next
return array

237. Delete Node in a Linked List

237. Delete Node in a Linked List

Medium1 solution1 playground

Solutions:

def deleteNode(node):
node.val = node.next.val
node.next = node.next.next

Search In Linked List

Search In Linked List

Basic1 solution1 playground

Solutions:

def searchLinkedList(head, x):
while head:
if head.data == x:
return True
head = head.next
return False

Is Linked List Length Even?

Is Linked List Length Even?

Basic1 solution1 playground

Solutions:

def isLengthEven(head):
length = 0
while head:
head = head.next
length += 1
return length % 2 == 0

Frequency in a Linked List

Frequency in a Linked List

Easy1 solution1 playground

Solutions:

def count(head, key):
count = 0
while head:
count += head.data == key
head = head.next
return count

Modular Node

Modular Node

Basic1 solution1 playground

Solutions:

def modularNode(head, k):
mod_node = None
curr = head
index = 1
while curr:
if index % k == 0:
mod_node = curr
curr = curr.next
index += 1
return mod_node.data if mod_node else -1

Remove Nodes

203. Remove Linked List Elements

203. Remove Linked List Elements

Easy2 solutions2 playgrounds

Solutions:

def removeElements(head, val):
sentinel = ListNode(None, next=head)
current_node = sentinel
while current_node and current_node.next:
if current_node.next.val == val:
current_node.next = current_node.next.next
else:
current_node = current_node.next
return sentinel.next

83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List


:::

Solutions:

def deleteDuplicates(head):
current_node = head
while current_node and current_node.next:
if current_node.val == current_node.next.val:
current_node.next = current_node.next.next
else:
current_node = current_node.next
return head

1474. Delete N Nodes After M Nodes of a Linked List

Easy1 solution1 playground

Solutions:

def deleteNodes(head, m, n):
sentinel = ListNode(None)
curr = sentinel
while curr:
for _ in range(m):
if curr:
curr = curr.next
for _ in range(n):
if curr:
curr.next = curr.next.next
return sentinel.next

Modify Nodes

2046. Sort Linked List Already Sorted Using Absolute Values

Medium1 solution1 playground

Solutions:

def sortLinkedList(head):
sentinel = ListNode(None, next=head)
current_node = sentinel.next
while current_node and current_node.next:
if current_node.next.val < 0:
next_node = current_node.next
current_node.next = next_node.next
next_node.next = sentinel.next
sentinel.next = next_node
else:
current_node = current_node.next
return sentinel.next

Counter

3063. Linked List Frequency

3063. Linked List Frequency

Easy2 solutions2 playgrounds

Solutions:

def frequenciesOfElements(head):
counter = Counter()
curr = head
while curr:
counter[curr.val] += 1
curr = curr.next
sentinel = ListNode(None)
curr = sentinel
for freq in counter.values():
curr.next = ListNode(freq)
curr = curr.next
return sentinel.nexts