some linked list hackerrank exercises
This commit is contained in:
parent
4f37196a7d
commit
8c5f7a20d8
|
|
@ -0,0 +1,152 @@
|
||||||
|
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 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 findMergeNode function below.
|
||||||
|
|
||||||
|
/*
|
||||||
|
* For your reference:
|
||||||
|
*
|
||||||
|
* SinglyLinkedListNode { int data; SinglyLinkedListNode next; }
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
// https://stackoverflow.com/questions/1594061/check-if-two-linked-lists-merge-if-so-where/14956113#14956113
|
||||||
|
static int FindMergeNode(SinglyLinkedListNode headA, SinglyLinkedListNode headB) {
|
||||||
|
SinglyLinkedListNode currentA = headA;
|
||||||
|
SinglyLinkedListNode currentB = headB;
|
||||||
|
|
||||||
|
// Do till the two nodes are the same
|
||||||
|
while (currentA != currentB) {
|
||||||
|
// If you reached the end of one list start at the beginning of the other one
|
||||||
|
// currentA
|
||||||
|
if (currentA.next == null) {
|
||||||
|
currentA = headB;
|
||||||
|
} else {
|
||||||
|
currentA = currentA.next;
|
||||||
|
}
|
||||||
|
// currentB
|
||||||
|
if (currentB.next == null) {
|
||||||
|
currentB = headA;
|
||||||
|
} else {
|
||||||
|
currentB = currentB.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currentB.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
|
||||||
|
|
||||||
|
int tests = scanner.nextInt();
|
||||||
|
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
|
||||||
|
|
||||||
|
for (int testsItr = 0; testsItr < tests; testsItr++) {
|
||||||
|
int index = scanner.nextInt();
|
||||||
|
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
|
||||||
|
|
||||||
|
SinglyLinkedList llist1 = new SinglyLinkedList();
|
||||||
|
|
||||||
|
int llist1Count = scanner.nextInt();
|
||||||
|
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
|
||||||
|
|
||||||
|
for (int i = 0; i < llist1Count; i++) {
|
||||||
|
int llist1Item = scanner.nextInt();
|
||||||
|
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
|
||||||
|
|
||||||
|
llist1.insertNode(llist1Item);
|
||||||
|
}
|
||||||
|
|
||||||
|
SinglyLinkedList llist2 = new SinglyLinkedList();
|
||||||
|
|
||||||
|
int llist2Count = scanner.nextInt();
|
||||||
|
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
|
||||||
|
|
||||||
|
for (int i = 0; i < llist2Count; i++) {
|
||||||
|
int llist2Item = scanner.nextInt();
|
||||||
|
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
|
||||||
|
|
||||||
|
llist2.insertNode(llist2Item);
|
||||||
|
}
|
||||||
|
|
||||||
|
SinglyLinkedListNode ptr1 = llist1.head;
|
||||||
|
SinglyLinkedListNode ptr2 = llist2.head;
|
||||||
|
|
||||||
|
for (int i = 0; i < llist1Count; i++) {
|
||||||
|
if (i < index) {
|
||||||
|
ptr1 = ptr1.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < llist2Count; i++) {
|
||||||
|
if (i != llist2Count - 1) {
|
||||||
|
ptr2 = ptr2.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr2.next = ptr1;
|
||||||
|
|
||||||
|
int result = findMergeNode(llist1.head, llist2.head);
|
||||||
|
|
||||||
|
bufferedWriter.write(String.valueOf(result));
|
||||||
|
bufferedWriter.newLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
bufferedWriter.close();
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
/*
|
||||||
|
Detect a cycle in a linked list. Note that the head pointer may be 'null' if the list is empty.
|
||||||
|
|
||||||
|
A Node is defined as:
|
||||||
|
class Node {
|
||||||
|
int data;
|
||||||
|
Node next;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Node {
|
||||||
|
int data;
|
||||||
|
Node next;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean hasCycle(Node head) {
|
||||||
|
Node slow = head;
|
||||||
|
Node fast = head;
|
||||||
|
while (fast != null && fast.next != null){
|
||||||
|
slow = slow.next;
|
||||||
|
fast = fast.next.next;
|
||||||
|
if(slow == fast) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,112 @@
|
||||||
|
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 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.
|
||||||
|
|
||||||
|
/*
|
||||||
|
* For your reference:
|
||||||
|
*
|
||||||
|
* SinglyLinkedListNode { int data; SinglyLinkedListNode next; }
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head, int data, int position) {
|
||||||
|
int i = 0;
|
||||||
|
SinglyLinkedListNode head_copy = head;
|
||||||
|
SinglyLinkedListNode new_node = new SinglyLinkedListNode(data);
|
||||||
|
while (head != null) {
|
||||||
|
if (i == position - 1) {
|
||||||
|
new_node.next = head.next;
|
||||||
|
head.next = new_node;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
head = head.next;
|
||||||
|
}
|
||||||
|
return head_copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,135 @@
|
||||||
|
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 DoublyLinkedListNode {
|
||||||
|
public int data;
|
||||||
|
public DoublyLinkedListNode next;
|
||||||
|
public DoublyLinkedListNode prev;
|
||||||
|
|
||||||
|
public DoublyLinkedListNode(int nodeData) {
|
||||||
|
this.data = nodeData;
|
||||||
|
this.next = null;
|
||||||
|
this.prev = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class DoublyLinkedList {
|
||||||
|
public DoublyLinkedListNode head;
|
||||||
|
public DoublyLinkedListNode tail;
|
||||||
|
|
||||||
|
public DoublyLinkedList() {
|
||||||
|
this.head = null;
|
||||||
|
this.tail = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertNode(int nodeData) {
|
||||||
|
DoublyLinkedListNode node = new DoublyLinkedListNode(nodeData);
|
||||||
|
|
||||||
|
if (this.head == null) {
|
||||||
|
this.head = node;
|
||||||
|
} else {
|
||||||
|
this.tail.next = node;
|
||||||
|
node.prev = this.tail;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.tail = node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printDoublyLinkedList(DoublyLinkedListNode 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 sortedInsert function below.
|
||||||
|
|
||||||
|
/*
|
||||||
|
* For your reference:
|
||||||
|
*
|
||||||
|
* DoublyLinkedListNode {
|
||||||
|
* int data;
|
||||||
|
* DoublyLinkedListNode next;
|
||||||
|
* DoublyLinkedListNode prev;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static DoublyLinkedListNode sortedInsert(DoublyLinkedListNode head, int data) {
|
||||||
|
DoublyLinkedListNode head_copy = head;
|
||||||
|
DoublyLinkedListNode new_node = new DoublyLinkedListNode(data);
|
||||||
|
|
||||||
|
if(head.data >= data){
|
||||||
|
new_node.next = head;
|
||||||
|
head.prev = new_node;
|
||||||
|
return new_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(head != null){
|
||||||
|
if (head.data >= data){
|
||||||
|
new_node.next = head;
|
||||||
|
new_node.prev = head.prev;
|
||||||
|
head.prev.next = new_node;
|
||||||
|
head.prev = new_node;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (head.next == null) {
|
||||||
|
head.next = new_node;
|
||||||
|
new_node.prev = head;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
head = head.next;
|
||||||
|
}
|
||||||
|
return head_copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
|
||||||
|
|
||||||
|
int t = scanner.nextInt();
|
||||||
|
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
|
||||||
|
|
||||||
|
for (int tItr = 0; tItr < t; tItr++) {
|
||||||
|
DoublyLinkedList llist = new DoublyLinkedList();
|
||||||
|
|
||||||
|
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])?");
|
||||||
|
|
||||||
|
DoublyLinkedListNode llist1 = sortedInsert(llist.head, data);
|
||||||
|
|
||||||
|
printDoublyLinkedList(llist1, " ", bufferedWriter);
|
||||||
|
bufferedWriter.newLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
bufferedWriter.close();
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,113 @@
|
||||||
|
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 DoublyLinkedListNode {
|
||||||
|
public int data;
|
||||||
|
public DoublyLinkedListNode next;
|
||||||
|
public DoublyLinkedListNode prev;
|
||||||
|
|
||||||
|
public DoublyLinkedListNode(int nodeData) {
|
||||||
|
this.data = nodeData;
|
||||||
|
this.next = null;
|
||||||
|
this.prev = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class DoublyLinkedList {
|
||||||
|
public DoublyLinkedListNode head;
|
||||||
|
public DoublyLinkedListNode tail;
|
||||||
|
|
||||||
|
public DoublyLinkedList() {
|
||||||
|
this.head = null;
|
||||||
|
this.tail = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertNode(int nodeData) {
|
||||||
|
DoublyLinkedListNode node = new DoublyLinkedListNode(nodeData);
|
||||||
|
|
||||||
|
if (this.head == null) {
|
||||||
|
this.head = node;
|
||||||
|
} else {
|
||||||
|
this.tail.next = node;
|
||||||
|
node.prev = this.tail;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.tail = node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printDoublyLinkedList(DoublyLinkedListNode 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 reverse function below.
|
||||||
|
|
||||||
|
/*
|
||||||
|
* For your reference:
|
||||||
|
*
|
||||||
|
* DoublyLinkedListNode { int data; DoublyLinkedListNode next;
|
||||||
|
* DoublyLinkedListNode prev; }
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static DoublyLinkedListNode reverse(DoublyLinkedListNode head) {
|
||||||
|
|
||||||
|
if (head == null) return null;
|
||||||
|
head.prev = head.next;
|
||||||
|
head.next = null;
|
||||||
|
while (head.prev != null){
|
||||||
|
head = head.prev;
|
||||||
|
DoublyLinkedListNode temp = head.next;
|
||||||
|
head.next = head.prev;
|
||||||
|
head.prev = temp;
|
||||||
|
}
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
|
||||||
|
|
||||||
|
int t = scanner.nextInt();
|
||||||
|
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
|
||||||
|
|
||||||
|
for (int tItr = 0; tItr < t; tItr++) {
|
||||||
|
DoublyLinkedList llist = new DoublyLinkedList();
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
DoublyLinkedListNode llist1 = reverse(llist.head);
|
||||||
|
|
||||||
|
printDoublyLinkedList(llist1, " ", bufferedWriter);
|
||||||
|
bufferedWriter.newLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
bufferedWriter.close();
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue