Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Insert_tail.java #28

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 70 additions & 0 deletions Data Structures/Linked Lists/Insert_at_tail.java
@@ -0,0 +1,70 @@
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

static class SinglyLinkedListNode {
public int data;
public SinglyLinkedListNode next;

public SinglyLinkedListNode(int nodeData) {
this.data = nodeData;
this.next = null;
}
}

static class SinglyLinkedList {
public SinglyLinkedListNode head;

public SinglyLinkedList() {
this.head = null;
}


}

public static void printSinglyLinkedList(SinglyLinkedListNode node, String sep, BufferedWriter bufferedWriter) throws IOException {
while (node != null) {
bufferedWriter.write(String.valueOf(node.data));

node = node.next;

if (node != null) {
bufferedWriter.write(sep);
}
}
}

// Complete the insertNodeAtTail function below.

/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode next;
* }
*
*/
static SinglyLinkedListNode insertNodeAtTail(SinglyLinkedListNode head, int data) {
SinglyLinkedListNode temp = new SinglyLinkedListNode(data);
SinglyLinkedListNode curr = head;

if(head==null){
head = temp;
return head;
} else {
while(curr.next!=null){
curr = curr.next;
}
curr.next = temp;
return head;
}
}

private static final Scanner scanner = new Scanner(System.in);