View on GitHub

SCRATCH PAD

No polish, just pulse

Striver’s SDE Sheet Notes (Day 6: LinkedList part-II)


1. Find Intersection Point of Y LinkedList

  public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
      if (headA == null || headB == null) return null;
      ListNode pA = headA, pB = headB;
      while (pA != pB) {
          pA = (pA == null) ? headB : pA.next;
          pB = (pB == null) ? headA : pB.next;
      }
      return pA;
  }
  

2. Detect a Cycle in Linked List

  public boolean hasCycle(ListNode head) {
      ListNode slow = head, fast = head;
      while (fast != null && fast.next != null) {
          slow = slow.next;
          fast = fast.next.next;
          if (slow == fast) return true;
      }
      return false;
  }
  

3. Reverse a Linked List in Groups of Size k

  public ListNode reverseKGroup(ListNode head, int k) {
      if (head == null || k == 1) return head;
      ListNode dummy = new ListNode(0);
      dummy.next = head;
      ListNode curr = dummy, nex = dummy, pre = dummy;
      int count = 0;
      while (curr.next != null) {
          curr = curr.next;
          count++;
      }
      while (count >= k) {
          curr = pre.next;
          nex = curr.next;
          for (int i = 1; i < k; i++) {
              curr.next = nex.next;
              nex.next = pre.next;
              pre.next = nex;
              nex = curr.next;
          }
          pre = curr;
          count -= k;
      }
      return dummy.next;
  }
  

4. Check if a Linked List is Palindrome or Not

  public boolean isPalindrome(ListNode head) {
      if (head == null || head.next == null) return true;
      ListNode slow = head, fast = head;
      while (fast != null && fast.next != null) {
          slow = slow.next;
          fast = fast.next.next;
      }
      slow = reverse(slow);
      fast = head;
      while (slow != null) {
          if (slow.val != fast.val) return false;
          slow = slow.next;
          fast = fast.next;
      }
      return true;
  }

  private ListNode reverse(ListNode head) {
      ListNode prev = null;
      while (head != null) {
          ListNode next = head.next;
          head.next = prev;
          prev = head;
          head = next;
      }
      return prev;
  }
  

5. Find the Starting Point of the Loop in a Linked List

  public ListNode detectCycle(ListNode head) {
      ListNode slow = head, fast = head;
      while (fast != null && fast.next != null) {
          slow = slow.next;
          fast = fast.next.next;
          if (slow == fast) {
              slow = head;
              while (slow != fast) {
                  slow = slow.next;
                  fast = fast.next;
              }
              return slow;
          }
      }
      return null;
  }
  

6. Flattening of a Linked List

  public Node flatten(Node head) {
      if (head == null) return null;
      Node current = head;
      while (current != null) {
          if (current.child != null) {
              Node temp = current.next;
              current.next = flatten(current.child);
              current.next.prev = current;
              current.child = null;
              while (current.next != null) {
                  current = current.next;
              }
              current.next = temp;
              if (temp != null) temp.prev = current;
          }
          current = current.next;
      }
      return head;
  }