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

Python:
class Solution:
    def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode:
        count = 0
        cur = list1
        temp1 = cur
        temp2 = cur
        while cur is not None:
            if count == a - 1:
                temp1 = cur
            if count == b:
                temp2 = cur.next
                break
            cur = cur.next
            count += 1
        temp3 = list2
        while temp3.next is not None:
            temp3 = temp3.next
        temp1.next = list2
        temp3.next = temp2
        return list1
 
Java:
class Solution {
    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        ListNode list2Tail = list2;
        ListNode list1Left = list1;
        ListNode list1Right = list1;
        ListNode pointer = list1;

        int idx = 0;

        while (list2Tail.next != null) {
            list2Tail = list2Tail.next;
        }

        while (pointer != null) {
            if (idx == a - 1) {
                list1Left = pointer;
            }

            if (idx == b + 1) {
                list1Right = pointer;
            }
            idx++;
            pointer = pointer.next;
        }

        list1Left.next = list2;
        list2Tail.next = list1Right;

        return list1;
    }
}
 
Gặp LinkedList là lay hoay mãi.
JavaScript:
var mergeInBetween = function(list1, a, b, list2) {
    let index = 0;
    let dummy = new ListNode(0, list1);
    let aNode = dummy;
    let bNode = dummy;
    
    while (index < a) {
        aNode = aNode.next;
        bNode = bNode.next;
        index++;
    }

    while (index <= b) {
        bNode = bNode.next;
        index++;
    }

    aNode.next = list2;

    while (aNode.next) {
        aNode = aNode.next;
    }

    aNode.next = bNode.next;

    return dummy.next;
};
 
Code:
ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
        ListNode* slow = list1;
        ListNode* fast = list1;
        for (int i = 0; i < b - a + 2; i++) {
            fast = fast->next;
        }

        int nth = 1;
        while (nth < a) {
            slow = slow->next;
            fast = fast->next;
            nth++;
        }

        slow->next = list2;
        ListNode* prev = slow;
        while (prev->next != nullptr) {
            prev = prev->next;
        }
        prev->next = fast;
        return list1;
    }
 
Bài hôm nay dễ thật, không có edge case luôn.

Code:
def merge_in_between(list1, a, b, list2)
    before_a = list1
    (a - 1).times { before_a = before_a.next }
    at_b = before_a
    (b - a + 1).times { at_b = at_b.next }
    before_a.next = list2
    
    list2 = list2.next while list2.next
    list2.next = at_b.next
    
    list1
end
 
Cho mình hỏi làm test trên codility thì làm xong câu nào bấm submit câu đó hay làm hết r mới bấm submit 1 lượt nhỉ các bác.
 
trước có bà chị kế toán nhờ làm phân tích tổng của các món tiền
đọc xong 1 lúc mới thấy y hệt bài hôm trước vừa làm trên leetcode
làm xong còn viết hẳn 1 cái demo web, chỉ việc ném file excel vào là nó tự ra kết quả
bà chị cầm đi khè cả cty :)) cười vãi
lần đầu tiên áp dụng được giải toán vào thực tế
 
lâu lâu được leetcode thả cho bài dễ

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} list1
* @param {number} a
* @param {number} b
* @param {ListNode} list2
* @return {ListNode}
*/
var mergeInBetween = function(list1, a, b, list2) {
let node = list1;
let i = 0;
while (i < a - 1) {
node = node.next;
++i;
}
let nodeSecondth = node.next;
node.next = list2;
while (i < b) {
nodeSecondth = nodeSecondth.next;
++i;
}
while (list2.next) list2 = list2.next;
list2.next = nodeSecondth;
return list1;
};
 
JavaScript:
function mergeInBetween(list1: ListNode | null, a: number, b: number, list2: ListNode | null): ListNode | null {
    let firstHalf: ListNode | null = list1;
    let lastHalf: ListNode | null = null;

    // Get first half
    for (let i = 1; i < a; i++) {
        firstHalf = firstHalf.next;
    }

    // Get last half
    lastHalf = firstHalf.next;
    for (let i = a; i <= b; i++) {
        lastHalf = lastHalf.next;
    }

    // Connect fist half with list2
    firstHalf.next = list2;
    while (list2.next) {
        list2 = list2.next;
    }

    // Connect list2 with last half
    list2.next = lastHalf;

    return list1
};
 
trước có bà chị kế toán nhờ làm phân tích tổng của các món tiền
đọc xong 1 lúc mới thấy y hệt bài hôm trước vừa làm trên leetcode
làm xong còn viết hẳn 1 cái demo web, chỉ việc ném file excel vào là nó tự ra kết quả
bà chị cầm đi khè cả cty :)) cười vãi
lần đầu tiên áp dụng được giải toán vào thực tế
Sao ko kể chuyện sau đó với chị kế toán fence, chuyện thằng em IT 96 đó :shame::shame:

via theNEXTvoz for iPhone
 
test trên đấy mở tab khác nó biết không nhỉ
Em cũng không biết nữa thím, đề bên công ty em apply có ghi rõ là cho xài IDE thoải mái nên cũng không quan tâm lắm. Nếu theo docs của nó thì là không có, còn thực tế ra sao thì phải chờ thím nào rảnh rỗi đi phỏng vấn dạo rồi reverse mấy đoạn code JS ra để trả lời thôi :big_smile:
 
Python:
class Solution:
    def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode:
        prev_a_node = list1
        b_node = list1

        for i in range(a - 1):
            prev_a_node = prev_a_node.next
        for i in range(b):
            b_node = b_node.next

        if prev_a_node != None:
            prev_a_node.next = list2

        while list2.next != None:
            list2 = list2.next
        list2.next = b_node.next
        
        return list1
 
Không thể nghĩ đc cách recursion :LOL:) Nhớ hồi sinh viên học môn cấu giải ngay buổi thực hành thứ 2 làm bài này cmnr

Code:
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev = None

        while head:
            next_node = head.next
            head.next = prev
            prev = head
            head = next_node
        
        return prev
 
Bài này làm rồi, lôi ra submit luôn
C++:
    ListNode* reverseList(ListNode* head) {
        ListNode *pre = new ListNode(0), *cur = head;
        pre -> next = head;
        while (cur && cur -> next) {
            ListNode* temp = pre -> next;
            pre -> next = cur -> next;
            cur -> next = cur -> next -> next;
            pre -> next -> next = temp;
        }
        return pre -> next;  
    }
 
21/02/2024: Bài này làm kiểu đệ quy nó mới nguy hiểm, :beauty:
C++:
class Solution {
public:
    ListNode* reverseList(ListNode* head, ListNode* prev = nullptr) {
        if (head == nullptr) return prev;
        auto next = head->next;
        head->next = prev;
        return reverseList(next, head);
    }
};
 
Last edited:
Back
Top