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

Deletelastnode

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

class DeleteLastNode {

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

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

  public static void printlist(Node head) {
    Node curr = head;
    while (curr != null) {
      System.out.print(curr.data + " ");
      curr = curr.next;
    }
    System.out.println();
  }
}

Created 2021-12-29T07:25:42+00:00 · Edit