Skip to content

Instantly share code, notes, and snippets.

@yoosinpaddy
Last active July 1, 2022 13:33
Show Gist options
  • Save yoosinpaddy/5edd1956f8c23589be78e959fb61f412 to your computer and use it in GitHub Desktop.
Save yoosinpaddy/5edd1956f8c23589be78e959fb61f412 to your computer and use it in GitHub Desktop.
Insert a node at a specific position in a linked list
package com.company;
import java.io.*;
import java.util.*;
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 SinglyLinkedListNode tail;
public SinglyLinkedList() {
this.head = null;
this.tail = null;
}
public void insertNode(int nodeData) {
SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}
this.tail = node;
}
}
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 'insertNodeAtPosition' function below.
*
* The function is expected to return an INTEGER_SINGLY_LINKED_LIST.
* The function accepts following parameters:
* 1. INTEGER_SINGLY_LINKED_LIST llist
* 2. INTEGER data
* 3. INTEGER position
*/
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode next;
* }
*
*/
static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position) {
// Write your code here
SinglyLinkedListNode copy = llist;
SinglyLinkedListNode toIns = new SinglyLinkedListNode(data);
if (position == 0) {
toIns.next = copy;
return toIns;
}
int pos = 0;
System.out.println("position:" + position);
boolean shouldC=true;
while (shouldC) {
System.out.println(pos);
if (pos == position) {
System.out.println("End:");
SinglyLinkedListNode toRep = new SinglyLinkedListNode(copy.data);
toRep.next=copy.next;
copy.data = toIns.data;
copy.next = toRep;
shouldC=false;
return llist;
} else {
if (copy.next != null) {
copy = copy.next;
} else {
shouldC=false;
}
}
pos++;
}
return llist;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = null;
try {
File f = new File("s.txt");
if (!f.exists()) {
System.out.println(f.createNewFile());
}
bufferedWriter = new BufferedWriter(new FileWriter(f.getName()));
} catch (IOException e) {
e.printStackTrace();
}
SinglyLinkedList llist = new SinglyLinkedList();
int llistCount = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < llistCount; i++) {
int llistItem = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
llist.insertNode(llistItem);
}
int data = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
int position = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
SinglyLinkedListNode llist_head = insertNodeAtPosition(llist.head, data, position);
printSinglyLinkedList(llist_head, " ", bufferedWriter);
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment