filipecancio / prep-tech-24

4 stars 0 forks source link

class 01 - Linked List #1

Open filipecancio opened 3 months ago

filipecancio commented 3 months ago

Leetcode exercises

Teacher's submitions

Click to see the code ```java /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode result = new ListNode(0); ListNode resultNext = result; int carry = 0; while(l1 != null || l2 != null || carry > 0) { int localSum = carry; if (l1 != null) { localSum += l1.val; l1 = l1.next; } if (l2 != null) { localSum += l2.val; l2 = l2.next; } resultNext.val = localSum % 10; carry = localSum / 10; if (l1 != null || l2 != null || carry > 0) { resultNext.next = new ListNode(0); resultNext = resultNext.next; } } return result; } } ```
Click to see the code ```python # Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ res_first = None res_last = None remain = 0 l1_node = l1 l2_node = l2 while l1_node != None and l2_node != None: sum = l1_node.val + l2_node.val + remain node = ListNode(sum % 10) if res_first == None: res_first = node if res_last != None: res_last.next = node res_last = node remain = sum / 10 l1_node = l1_node.next l2_node = l2_node.next if l1_node != None: l_node = l1_node elif l2_node != None: l_node = l2_node else: l_node = None while l_node != None: sum = l_node.val + remain node = ListNode(sum % 10) res_last.next = node res_last = node remain = sum / 10 l_node = l_node.next if remain > 0: node = ListNode(remain) res_last.next = node res_last = node return res_first ```
filipecancio commented 3 months ago

Mine solutions for 876. Middle of the Linked List

Kotlin solution ```kotlin /** * Example: * var li = ListNode(5) * var v = li.`val` * Definition for singly-linked list. * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ class Solution { fun middleNode(head: ListNode?): ListNode? { var i = head var j = head while (i?.next?.next != null){ j = j?.next i = i?.next?.next } if(i?.next != null) j = j?.next return j } } ```
Python solution ```python3 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: if head == None or head.next == None: return head i = head j = head while i.next != None and i.next.next != None: j = j .next i = i.next.next if i.next != None: j = j.next return j ```
Dart solution ```dart /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode? next; * ListNode([this.val = 0, this.next]); * } */ class Solution { ListNode? middleNode(ListNode? head) { ListNode? i = head; ListNode? j = head; while (i?.next?.next != null) { j = j?.next; i = i?.next?.next; } if( i?.next != null) j = j?.next; return j; } } ```
filipecancio commented 3 months ago

Mine solutions for 206. Reverse Linked List

Kotlin solution ```kotlin /** * Example: * var li = ListNode(5) * var v = li.`val` * Definition for singly-linked list. * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ class Solution { fun reverseList(head: ListNode?): ListNode? { var current = head var previous : ListNode? = null while(current != null){ val next = current?.next current?.next = previous previous = current current = next } return previous } } ```
Python solution ```python3 # 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]: previous = None current = head while current != None : next = current.next current.next = previous previous = current current = next return previous ```
Dart solution ```dart /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode? next; * ListNode([this.val = 0, this.next]); * } */ class Solution { ListNode? reverseList(ListNode? head) { ListNode? previous = null; ListNode? current = head; while(current != null){ ListNode? next = current?.next; current?.next = previous; previous = current; current = next; } return previous; } }
filipecancio commented 3 months ago

Mine solutions for 2. Add Two Numbers

Kotlin solution ```kotlin /** * Example: * var li = ListNode(5) * var v = li.`val` * Definition for singly-linked list. * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ class Solution { fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? { var pointerOne = l1 var pointerTwo = l2 var result = ListNode(0) var current = result var module = 0 while (pointerOne != null || pointerTwo != null || module != 0) { val sum = (pointerOne?.`val` ?: 0) + (pointerTwo?.`val` ?: 0) + module module = sum/10 current.next = ListNode(sum%10) current = current.next pointerOne = pointerOne?.next pointerTwo = pointerTwo?.next } return result.next } } ```