Skip to content

Instantly share code, notes, and snippets.

@alexmurphas8
alexmurphas8 / Excel Sheet Column Number.py
Created September 5, 2022 09:50
LeetCode 171. Excel Sheet Column Number
class Solution(object):
def titleToNumber(self, columnTitle):
sum = 0
for i in range(len(columnTitle)):
sum = (sum*26)+(ord(columnTitle[i])-64)
return sum
@alexmurphas8
alexmurphas8 / Excel Sheet Column Number.java
Created September 5, 2022 09:46
LeetCode 171. Excel Sheet Column Number
class Solution {
public int titleToNumber(String columnTitle) {
if (columnTitle == null) return -1;
int sum = 0;
for(int i = 0; i < columnTitle.length(); i++){
sum = ((sum*26) + ((int)columnTitle.charAt(i) - 64));
}
return sum;
}
}
@alexmurphas8
alexmurphas8 / LeetCode 169. Majority Element.py
Created September 2, 2022 10:00
LeetCode 169. Majority Element
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count, res = 0,0
for num in nums:
if count == 0:
res = num
@alexmurphas8
alexmurphas8 / LeetCode 169. Majority Element.java
Created September 2, 2022 09:55
LeetCode 169. Majority Element
class Solution {
public int majorityElement(int[] nums) {
int count=0, res = 0;
for (int num: nums) {
if (count==0) {
res = num;
}
if (num!=ret) {
count--;
@alexmurphas8
alexmurphas8 / LeetCode 160. Intersection of Two Linked Lists.py
Created August 29, 2022 12:22
LeetCode 160. Intersection of Two Linked Lists
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
# If any one of head is null i.e
# no Intersection Point
if headA == None or headB == None:
return None
@alexmurphas8
alexmurphas8 / 160. Intersection of Two Linked Lists.java
Created August 29, 2022 12:16
LeetCode 160. Intersection of Two Linked Lists
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
// If any one of head is null i.e
// no Intersection Point
if(headA == null || headB == null){
return null;
}
// Maintaining two pointers nodeA and nodeB
@alexmurphas8
alexmurphas8 / 142. Linked List Cycle II.py
Created August 29, 2022 07:55
LeetCode 142. Linked List Cycle II
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def detectCycle(self, head):
slow = fast = head
# Do a loop using
@alexmurphas8
alexmurphas8 / 142. Linked List Cycle II.java
Last active August 29, 2022 07:54
LeetCode 142. Linked List Cycle II
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
// Search it using loop
// slow and fast pointers
while(fast!=null && fast.next!=null){
// Move slow and fast 1
// and 2 steps ahead
@alexmurphas8
alexmurphas8 / 141. Linked List Cycle.py
Created August 27, 2022 09:54
LeetCode 141. Linked List Cycle
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
@alexmurphas8
alexmurphas8 / 141. Linked List Cycle.java
Created August 27, 2022 09:51
LeetCode 141. Linked List Cycle
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow=head;
ListNode fast=head;
while(fast!=null && fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow){
return true;
}