Linked List Insertion At End - GFG Problem

 Given the head of a Singly Linked List and a value x, insert that value x at the end of the LinkedList and return the modified Linked List.


Examples :


Input: LinkedList: 1->2->3->4->5 , x = 6

Output: 1->2->3->4->5->6

Explanation: 


We can see that 6 is inserted at the end of the linkedlist.

Input: LinkedList: 5->4 , x = 1

Output: 5->4->1

Explanation: 


We can see that 1 is inserted at the end of the linkedlist.

Expected Time Complexity: O(n)

Expected Auxiliary Space: O(1)


Solution :



//{ Driver Code Starts

// Initial Template for Java


import java.io.*;

import java.util.*;


class Node {

    int data;

    Node next;


    Node(int x) {

        data = x;

        next = null;

    }

}



// } Driver Code Ends

/*

class Node{

    int data;

    Node next;


    Node(int x){

        data = x;

        next = null;

    }

}

*/

class Solution {

    // Function to insert a node at the end of the linked list.

    Node insertAtEnd(Node head, int x) {

        // code here

   Node newNode = new Node(x);


        if (head == null) {

            return newNode; // If list is empty, new node becomes head

        }


        Node tail = head;


        while (tail.next != null) { // Traverse to the last node

            tail = tail.next;

        }


        tail.next = newNode; // Append new node at the end

        return head; // Return the updated head

    }

}


//{ Driver Code Starts.


public class GFG {

    static void printList(Node node) {

        while (node != null) {

            System.out.print(node.data + " ");

            node = node.next;

        }

        System.out.println();

    }


    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int t = Integer.parseInt(br.readLine().trim());


        while (t-- > 0) {

            List<Integer> arr = new ArrayList<>();

            String input = br.readLine().trim();

            if (!input.isEmpty()) {

                String[] numbers = input.split("\\s+");

                for (String num : numbers) {

                    if (!num.isEmpty()) {

                        arr.add(Integer.parseInt(num));

                    }

                }

            }


            int x = Integer.parseInt(br.readLine().trim());


            Node head = null;

            if (!arr.isEmpty()) {

                head = new Node(arr.get(0));

                Node tail = head;

                for (int i = 1; i < arr.size(); ++i) {

                    tail.next = new Node(arr.get(i));

                    tail = tail.next;

                }

            }


            Solution ob = new Solution();

            Node ans = ob.insertAtEnd(head, x);

            printList(ans);

        

System.out.println("~");

}

    }

}

// } Driver Code Ends




Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence