Leetcode 2487. Remove Nodes From Linked List

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

here is iteratively
class Solution:
def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
# reverse the list
pre, cur = None, head
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp

# remove the node that is less than the max(greater value to the right)
head = pre
cur = pre.next
max = pre.val
while cur:
if cur.val < max:
pre.next = cur.next
cur.next = None
cur = pre.next
else:
max = cur.val
pre = cur
cur = cur.next

# reverse the list again
pre, cur = None, head
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre

aynuayex
Автор

This video is amazing, it is much better to have someone explain the concept on whiteboard and then code it themselves than watching other people pasting code and trying to explain it at once.

pamix
Автор

great explaination ! but I've got a question - the given is a singly linked list, it wouldn't have a "prev" pointer, so how can we traverse from the right to left?

bySahej
join shbcf.ru