rioxshen
rioxshen
Reputation Top 1%
Rio Shen
8 Snippets  (104th place)
Published
2 Channels
Created
1 Channel
Following
210 points  (141st place)
Reputation
Junior Code Generator
Serious Code Generator
Junior Publisher
Serious Publisher
Junior Topic Creator
Junior Topic Hub
Junior Trend Maker
Junior Popular Coder
Serious Popular Coder
Junior Autobiographer
Senior Autobiographer
Serious Famous Coder

Recent Snippets See all snippets by rioxshen

public by rioxshen  2160  3  6  0

[LeetCode] Remove duplicates from sorted list - Set

public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        
        ListNode current = head;
        Set<Integer> set = new HashSet<>();
        set.add(current.val);
        while (current.next != null) {
            if (!set.add(current.next.val)) {
                current.next = current.next.next;
            } else {
                current = current.next;
            }
        }
        
        return head;
    }
}
;

public by rioxshen  2149  2  6  0

[LeetCode] remove duplicates from sorted list

public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        
        ListNode current = head;
        while (current.next != null) {
            if (current.val == current.next.val) {
                current.next = current.next.next;
            } else {
                current = current.next;
            }
        }
        
        return head;
    }
}
;

public by rioxshen  1808  6  6  1

[LeetCode] Partition Problem

Partition a list around the value x.
public class Solution {
    public ListNode partition(ListNode head, int x) {
        if (head == null || head.next == null) return head;
        
        ListNode leftDummy = new ListNode(Integer.MIN_VALUE);
        ListNode rightDummy = new ListNode(Integer.MIN_VALUE);
        ListNode left = leftDummy;
        ListNode right = rightDummy;
        
        while (head != null) {
            if (head.val < x) {
                left.next = head;
                left = left.next;
            } else {
                right.next = head;
                right = right.next;
            }
            head = head.next;
        }
        
        left.next = rightDummy.next;
        right.next = null;
        
        return leftDummy.next;
    }
}
;

public by rioxshen  1819  1  6  1

AddTwoNumbers.java

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int carry = 0;
        ListNode dummy = new ListNode(Integer.MIN_VALUE);
        ListNode current = dummy;
    
        while (l1 != null || l2 != null) {
            int a = l1 == null ? 0 : l1.val;
            int b = l2 == null ? 0 : l2.val;
            int sum = a + b + carry;
            current.next = new ListNode(sum % 10);
            
            carry = sum / 10;
            l1 = l1 == null ? null : l1.next;
            l2 = l2 == null ? null : l2.next;
            current = current.next;
        }
        
        if (carry > 0) current.next = new ListNode(carry);
        
        return dummy.next;
    }
}
;

public by rioxshen  2188  0  6  0

[leetcode] Insert Interval

class Interval:
  def __init__(self, s=0, e=0):
    self.start = s
    self.end = e


class Solution:
  def insert(self, intervals, newIntervals):
    """ Inserts the new interval into the list and
        returns merged intervals.
    
    1. Appends the new interval to the list.
    2. Sorts the list.
    3. Iterates through and merges the new interval list.
    """
    if not intervals:
      return [newInterval]
    
    intervals.append(newInterval)
    intervals.sort(lambda x, y: x.start - y.start)
    
    result = []
    length = len(intervals)
    last = intervals[0]
    for i in range(1, length):
      current = intervals[i]
      if current.start <= last.end: # Merge intercetions
        last.end = max(last.end, current.end)
      else:
        result.append(last)
        last = current
    result.append(last)
    
    return result
        
;