Last active
June 30, 2022 09:33
-
-
Save yoosinpaddy/04114741bf05c507c0950bf9cc05f658 to your computer and use it in GitHub Desktop.
Reverse Double linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
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 Main { | |
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. | |
* | |
* The function is expected to return an INTEGER_DOUBLY_LINKED_LIST. | |
* The function accepts INTEGER_DOUBLY_LINKED_LIST llist as parameter. | |
*/ | |
/* | |
* For your reference: | |
* | |
* DoublyLinkedListNode { | |
* int data; | |
* DoublyLinkedListNode next; | |
* DoublyLinkedListNode prev; | |
* } | |
* | |
*/ | |
static DoublyLinkedListNode reverse(DoublyLinkedListNode llist) { | |
// Write your code here | |
DoublyLinkedListNode t = llist; | |
DoublyLinkedListNode pr=null; | |
while(t!=null){ | |
DoublyLinkedListNode nx=t.next; | |
t.prev=nx; | |
t.next=pr; | |
pr=t; | |
t=nx; | |
} | |
return llist; | |
} | |
private static final Scanner scanner = new Scanner(System.in); | |
public static void main(String[] args) { | |
/** | |
* Sample input | |
* 1 | |
* 4 | |
* 1 | |
* 2 | |
* 3 | |
* 4 | |
**/ | |
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(); | |
} | |
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); | |
try { | |
printDoublyLinkedList(llist1, " ", bufferedWriter); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
try { | |
bufferedWriter.newLine(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
try { | |
bufferedWriter.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
scanner.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment