View on GitHub

SCRATCH PAD

No polish, just pulse

Striver’s SDE Sheet Notes (Day 7: LinkedList and Arrays)


1. Rotate a LinkedList

  public ListNode rotateRight(ListNode head, int k) {
      if (head == null || head.next == null || k == 0) return head;
      ListNode temp = head;
      int length = 1;
      while (temp.next != null) {
          temp = temp.next;
          length++;
      }
      temp.next = head;  // Make the list circular
      k = k % length;
      for (int i = 0; i < length - k; i++) {
          temp = temp.next;
      }
      head = temp.next;
      temp.next = null;  // Break the circle
      return head;
  }
  

2. Clone a Linked List with Random and Next Pointer

  public Node copyRandomList(Node head) {
      if (head == null) return null;
      Map<Node, Node> map = new HashMap<>();
      Node current = head;
      while (current != null) {
          map.put(current, new Node(current.val));
          current = current.next;
      }
      current = head;
      while (current != null) {
          map.get(current).next = map.get(current.next);
          map.get(current).random = map.get(current.random);
          current = current.next;
      }
      return map.get(head);
  }
  

3. 3 Sum

  public List<List<Integer>> threeSum(int[] nums) {
      List<List<Integer>> result = new ArrayList<>();
      Arrays.sort(nums);
      for (int i = 0; i < nums.length - 2; i++) {
          if (i > 0 && nums[i] == nums[i - 1]) continue;
          int left = i + 1, right = nums.length - 1;
          while (left < right) {
              int sum = nums[i] + nums[left] + nums[right];
              if (sum == 0) {
                  result.add(Arrays.asList(nums[i], nums[left], nums[right]));
                  while (left < right && nums[left] == nums[left + 1]) left++;
                  while (left < right && nums[right] == nums[right - 1]) right--;
                  left++;
                  right--;
              } else if (sum < 0) {
                  left++;
              } else {
                  right--;
              }
          }
      }
      return result;
  }
  

4. Trapping Rainwater

  public int trap(int[] height) {
      if (height == null || height.length == 0) return 0;
      int left = 0, right = height.length - 1, leftMax = 0, rightMax = 0, result = 0;
      while (left <= right) {
          if (height[left] <= height[right]) {
              if (height[left] >= leftMax) leftMax = height[left];
              else result += leftMax - height[left];
              left++;
          } else {
              if (height[right] >= rightMax) rightMax = height[right];
              else result += rightMax - height[right];
              right--;
          }
      }
      return result;
  }
  

5. Remove Duplicates from Sorted Array

  public int removeDuplicates(int[] nums) {
      if (nums.length == 0) return 0;
      int index = 1;
      for (int i = 1; i < nums.length; i++) {
          if (nums[i] != nums[i - 1]) {
              nums[index] = nums[i];
              index++;
          }
      }
      return index;
  }
  

6. Max Consecutive Ones

  public int findMaxConsecutiveOnes(int[] nums) {
      int maxCount = 0, currentCount = 0;
      for (int num : nums) {
          if (num == 1) {
              currentCount++;
              maxCount = Math.max(maxCount, currentCount);
          } else {
              currentCount = 0;
          }
      }
      return maxCount;
  }