thảo luận Leetcode mỗi ngày

Python:
class Solution:
    def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head.next:
            return head
        next_node = self.removeNodes(head.next)
        if head.val < next_node.val:
            return next_node
        head.next = next_node
        return head
 
C-like:
class Solution {
    fun removeNodes(head: ListNode?): ListNode? {
        val reversedNodes = reversedLinkedList(head)
        var maxValue = Int.MIN_VALUE
        var newHead = head
        for (i in 0 until reversedNodes.size) {
            val node = reversedNodes[i]
            if (node.`val` < maxValue) {
                val prevNode = reversedNodes.getOrNull(i + 1)
                if (prevNode == null) {
                    newHead = node.next
                } else {
                    prevNode.next = node.next
                }
                node.next = null
            } else {
                maxValue = node.`val`
            }
        }
        return newHead
    }

    private fun reversedLinkedList(head: ListNode?): List<ListNode> {
        val reversedNodes = mutableListOf<ListNode>()
        var ptr = head
        while (ptr != null) {
            reversedNodes.add(ptr!!)
            ptr = ptr?.next
        }
        return reversedNodes.asReversed()
    }
}
 
Bài này nếu làm theo kiểu reverse list thì sẽ nhanh hơn.
Mà không có thời gian

Java:
public ListNode removeNodes2(ListNode head) {
        Stack<ListNode> stack = new Stack<>();
        while (head != null) {
            if (stack.empty()) {
                stack.push(head);
                head = head.next;
            } else {
                ListNode top = stack.peek();
                if (head.val > top.val) {
                    stack.pop();
                } else {
                    top.next = head;
                    stack.push(head);
                    head = head.next;
                }
            }
        }

        return stack.firstElement();
    }
 
C#:
public class Solution
{
    public ListNode RemoveNodes(ListNode head)
    {
        Stack<ListNode> st = new();
        ListNode pointer = head;

        while (pointer != null)
        {
            if (st.Count == 0)
            {
                st.Push(pointer);
                pointer = pointer.next;
                continue;
            }

            while (st.Count > 0 && st.Peek().val < pointer.val)
            {
                st.Pop();
            }
            st.Push(pointer);
            pointer = pointer.next;
        }

        ListNode[] arr = st.ToArray();
        ListNode result = new(0, null);
        ListNode dummy = result;
        for (int i = arr.Length - 1; 0 <= i; i--)
        {
            dummy.next = arr[i];
            dummy = dummy.next;
        }
        dummy.next = null;

        return result.next;
    }
}
 
Java:
class Solution {
    public ListNode removeNodes(ListNode head) {
        ListNode pointer = head;
        ListNode ans = null;
        
        Stack<ListNode> stack = new Stack<>();

        while (pointer != null) {
            while (!stack.isEmpty() && pointer.val > stack.peek().val) {
                stack.pop();
            }

            stack.push(pointer);
            pointer = pointer.next;
        }
        
        while (!stack.isEmpty()) {
            ListNode temp = ans;
            ans = stack.pop();
            ans.next = temp;
        }

        return ans;
    }
}
 
Ruby:
# Definition for singly-linked list.
# class ListNode
#     attr_accessor :val, :next
#     def initialize(val = 0, _next = nil)
#         @val = val
#         @next = _next
#     end
# end
# @param {ListNode} head
# @return {ListNode}

def remove_nodes(head)
    to_be_deleted = []
    mono_stack = IncreasingStack.new
    mono_stack.push(head)

    current = head.next

    while current do
        popped = mono_stack.push(current)     
        to_be_deleted.concat(popped.map { |node| node.object_id })

        current = current.next
    end

    to_be_deleted = to_be_deleted.to_set
    sentinel = ListNode.new(0, head)
    current = sentinel

    while current && current.next do
        skip = current.next

        if to_be_deleted === skip.object_id
            current.next = skip.next
            skip.next = nil

            next
        else
            current = current.next
        end
    end

    head = sentinel.next
    head
end

class IncreasingStack
    attr_accessor :stack

    def initialize
        @stack = []
    end

    def push(node)
        popped = []
        while self.peek && self.peek.val < node.val
            popped << self.pop
        end

        stack.unshift(node)

        popped
    end

    def pop
        stack.shift
    end

    def peek
        stack[0]
    end

    def to_s
        "[#{stack.map(&:to_s).join(', ')}]"
    end
end

class ListNode
    def to_s
        "<##{self.object_id} val: #{@val}, next: #{@next.object_id}>"
    end
end
 
Last edited:
JavaScript:
var removeNodes = function(head) {
    let stack = [];

    let node = head;
    while (node) {
        while (stack.length > 0 && stack[stack.length - 1].val < node.val) {
            let prevNode = stack.pop();
            prevNode.next = null;
        }

        if (stack.length > 0) {
            stack[stack.length - 1].next = node;
        }

        stack.push(node);
        node = node.next;
    }

    return stack[0];
};
 
Ruby:
# Definition for singly-linked list.
# class ListNode
#     attr_accessor :val, :next
#     def initialize(val = 0, _next = nil)
#         @val = val
#         @next = _next
#     end
# end
# @param {ListNode} head
# @return {ListNode}

def remove_nodes(head)
    stack_bottom = nil
    stack_head = stack_bottom

    current = head

    while current
        while stack_head && current.val > stack_head.val
            skip = stack_head.next
            stack_head.next = nil
            stack_head = skip
        end

        skip = current.next
        current.next = stack_head
        stack_head = current

        current = skip
    end

    reversed_head = nil
    while stack_head
        skip = stack_head.next
        stack_head.next = reversed_head
        reversed_head = stack_head
        stack_head = skip
    end

    reversed_head
end
 
C#:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode RemoveNodes(ListNode head) {
        ListNode current = head.next;
        head.next = null;
        while(current != null)
        {
            ListNode temp = current.next;
            current.next = head;
            head = current;
            current = temp;
        }

        current = head;
        while(current.next != null)
        {
            if(current.val > current.next.val)
                current.next = current.next.next;
            else
                current = current.next;
        }

        current = head.next;
        head.next = null;
        while(current != null)
        {
            ListNode temp = current.next;
            current.next = head;
            head = current;
            current = temp;
        }
        
        return head;
    }
}
 
Java:
class Solution {
    public ListNode removeNodes(ListNode head) {
        head = this.reversedListNode(head);
        ListNode tmp = head;
        while(tmp != null && tmp.next != null){
            if(tmp.val >  tmp.next.val)
                tmp.next = tmp.next.next;
            else
                tmp = tmp.next;
        }
        head = this.reversedListNode(head);
        return head;
    }

    private ListNode reversedListNode(ListNode head){
        ListNode preNode = null;
        while(head.next != null){
            ListNode tmp = head.next;
            head.next = preNode;
            preNode = head;
            head = tmp;
        }
        head.next = preNode;
        return head;
    }
}
 
Java:
class Solution {
    public ListNode removeNodes(ListNode head) {
        ListNode pointer = head;
        ListNode ans = null;
       
        Stack<ListNode> stack = new Stack<>();

        while (pointer != null) {
            while (!stack.isEmpty() && pointer.val > stack.peek().val) {
                stack.pop();
            }

            stack.push(pointer);
            pointer = pointer.next;
        }
       
        while (!stack.isEmpty()) {
            ListNode temp = ans;
            ans = stack.pop();
            ans.next = temp;
        }

        return ans;
    }
}
hum nay đổi ava làm tìm mãi ko ra
s80uLz1.png
cứ tưởng bỏ cuộc chơi rùi
 
1715008533720.png

Hết áo rồi nhỉ :(

JavaScript:
var removeNodes = function(head) {
    const st = [];
    while (head) {
        while (st.length > 0 && st[st.length-1] < head.val) {
            st.pop();
        }
        st.push(head.val);
        head = head.next;
    }
    let root = new ListNode(), node = root;
    for (const it of st) {
        node.next = new ListNode(it);
        node = node.next;
    }
    return root.next;
};
 
View attachment 2478926
Hết áo rồi nhỉ :(

JavaScript:
var removeNodes = function(head) {
    const st = [];
    while (head) {
        while (st.length > 0 && st[st.length-1] < head.val) {
            st.pop();
        }
        st.push(head.val);
        head = head.next;
    }
    let root = new ListNode(), node = root;
    for (const it of st) {
        node.next = new ListNode(it);
        node = node.next;
    }
    return root.next;
};
chắc phải daily với weekly, biweekly cả năm mới đủ nhỉ bác
LrUPURL.gif
 
Python:
class Solution:
    def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
        
        def next(node):
            if node is None:
                return 0
            node.val = node.val * 2 + next(node.next)
            node.val, mod = node.val % 10, node.val // 10

            return mod
        
        mod = next(head)

        if mod > 0:
            node = ListNode(1)
            node.next = head
            head = node
        return head
 
Back
Top