Leet code practice

class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode().        ##create a new node
        cur = dummy                # point a variable to that new

        while list1 and list2:        #simpley put the list name which we are iterating onto
            if list1.val > list2.val: #
                cur.next = list2
                list2 = list2.next
            else:
                cur.next = list1
                list1 = list1.next   ##point to the next one
            
            cur = cur.next
        
        if list1:
            cur.next = list1
        else:
            cur.next = list2
        
        return dummy.next

or we can do

  • Binary to int

  • stairs (DP)

  • linekd list

don't use while(temp.next), instead of this use while temp and temp.next:

  • For recursion don't pass values which are changing into the recursive function, instead use this

for two pointers this should be the case

while i < len(nums) - 2:

  • train arrival time [ sorting 2 arrays which are dependent on each]

Steps

  1. Sort arrival times

  2. Sort departure times

  3. Use two pointers:

    • i → arrival

    • j → departure

  4. Traverse events in time order

  • power calculation

    • while calculating for power just calculate for the half and then multiply them , this saves calculations

these two functions give the left most and right most occurance of the number

  • classic binary search to find no of rotations

level order traversal

Last updated