code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

Deleteheadofcircularlinkedlist

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class DeleteHeadOfCircularLinkedList {

  static Node delHead(Node head) {
    if (head == null) {
      return null;
    }
    if (head.next == null) {
      return null;
    }
    Node current = head;
    while (current.next != head) {
      current = current.next;
    }
    current.next = head.next;
    return current.next;
  }

  public static void main(String[] args) {
    Node head = new Node(10);
    head.next = new Node(20);
    head.next.next = new Node(30);
    head.next.next.next = new Node(40);
    head.next.next.next.next = head;
    head = delHead(head);
    printlist(head);
  }

  public static void printlist(Node head) {
    if (head == null)
      return;
    Node r = head;
    do {
      System.out.print(r.data + " ");
      r = r.next;
    } while (r != head);
  }
}

Created 2021-12-31T00:36:08+00:00, updated 2021-12-31T07:44:54+00:00 · History · Edit