Linked List Reversal - java

My code for linked list reversal doesn't compile. Everything seems to be logically correct.
Here is the snippet of my LinkedList class:
public class LinkedList {
Node head;
public static class Node {
int data;
Node next;
Node(int d) {
this.data = d;
this.next = null;
}
}
public void display(Node n) {
n = head;
int ctr;
while (n != null) {
System.out.print(n.data +" ");
n = n.next;
}
ctr = countNodes(head);
System.out.println("The list length is "+ctr);
}
public Node recreverse(Node n){
Node prev = null;
Node temp = null;
if (n.next == null) {
head = n;
head.next = prev;
return head;
} else {
temp = n;
n = n.next;
prev = n;
prev.next = temp;
return n;
}
}
}

countNodes(Node) is not defined anywhere:
ctr = countNodes(head);
Define it and the code will compile.

Related

JAVA- Printing out LinkedLists in an ArrayList [duplicate]

This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 1 year ago.
I am trying to print out the elements in my ArrayList that looks like
static ArrayList<LinkedList> listy = new ArrayList<>();
I tried to create a function called PrintTest()
public static void pTest() {
String [] top;
for (LinkedList i: listy) {
//i.show();
System.out.println(i.toString());
However, I am still getting when I call printTest()
LinkedList#15975490
LinkedList#6b143ee9
LinkedList#1936f0f5
LinkedList#6615435c
LinkedList#4909b8da
LinkedList#3a03464
LinkedList#2d3fcdbd
LinkedList#617c74e5
Do I need to iterator over this once more? I am confused on how to go about this. Can I override toString()? I can't seem to get it to work
Here is my linkedlist implementation code
public class LinkedList {
Node head;
Node tail;
public String getFirst() {
Node node = head;
if (node.next == null) {
throw new NoSuchElementException();
}
else {
return node.data;
}
}
public void insert(String data) {
Node node = new Node();
node.data = data;
node.next = null;
if (head == null) {
head = node;
}
else {
Node n = head;
while(n.next !=null) {
n = n.next;
}
n.next = node;
}
}
public void insertAtStart(String data) {
Node node = new Node();
node.data = data;
node.next = null;
node.next = head;
head = node;
}
public void insertAt(int index, String data) {
Node node = new Node();
node.data = data;
node.next = null;
if(index == 0) {
insertAtStart(data);
}
else {
Node n = head;
for (int i = 0; i < index-1; i++) {
n = n.next;
}
node.next = n.next;
n.next = node;
}
}
public void deleteAt(int index) {
if (index == 0) {
head = head.next;
}
else {
Node n = head;
Node n1 = null;
for (int i = 0; i < index-1; i++) {
n = n.next;
}
n1 = n.next;
n.next = n1.next;
//System.out.println("n1 " + n1.data);
n1 = null;
}
}
public int size() {
int count =0;
Node pos = head;
while (pos != null) {
count++;
pos = pos.next;
}
return count;
}
public void remove(String s) {
Node node = head;
while (!node.data.equals(s)) {
node = node.next;
}
if (node.next == null) {
node.data = null;
}
else {
node.data = node.next.data;
node.next = node.next.next;
}
//System.out.println("n1 " + n1.data);
}
public void show() {
Node node = head;
while(node.next != null) {
System.out.println(node.data);
node = node.next;
}
System.out.println(node.data);
In java, every class inherits the Object class. In the Object class default definition for the toString method is hashcode. When you are calling toString method, you need to define by yourself.
class LinkedList{
int value;
#Override
public String toString(){
return String.valueOf(value);
}
}
I think you can go through this article to get more understanding.Explanation For toString

How to reverse a LinkedList from a certain index, given the code fragments below

I would like to write a method public void reverseFrom(int index) which reverses a list from the given index.
I would like to only use the LinkedList class below.
public class LinkedList {
public Node head = null;
public class Node {
public int value;
public Node next;
Node(int value, Node next) {
this.value = value
this.next = next;
}
}
}
I have a method to reverse a LinkedList:
public void reverse() {
Node tmp = head;
Node prev = null;
Node nextNode = null;
while(tmp != null) {
nextNode = tmp.next;
tmp.next = prev;
prev = tmp;
tmp = nextNode;
}
head = prev;
}
So far, for the reverseFrom method, I have:
public void reverseFrom(int index) {
if(index == 0) {
reverse();
} else if (index == 1) {
return;
} else {
Node tmp = head;
for(int i = 0; i < index; i++) {
tmp = tmp.next;
}
Node newHead = tmp;
/*** Node prev = null;
Node nextNode = null;
while(newHead != null) {
nextNode = newHead.next;
newHead.next = prev;
prev = newHead;
newHead = nextNode;
}
newHead = prev; ***/
}
}
I have tried using the code from reverse() but it does not work (that which is commented out).
How can I then reverse the list from newHead?
You could base the logic on the reverse method, but use an extra reference to the node that is at index - 1. During the loop decrement index. As long as it is positive, don't change the next reference of the current node. When it hits zero, take note of where we start the reversal, and as the current node will become the very last one, set its next reference to null. When index is negative, perform the usual reversal logic.
After the loop, check whether we need to change the head or whether we need to attach the reversed part to the node at index-1:
public void reverseFrom(int index) {
Node tmp = head;
Node prev = null;
Node nextNode = null;
Node tail = null; // node at index - 1
while (tmp != null) {
nextNode = tmp.next;
if (index == 0) {
// We arrived at the part that needs reversal
tmp.next = null;
tail = prev;
} else if (index < 0) {
// Perform normal reversal logic
tmp.next = prev;
}
index--;
prev = tmp;
tmp = nextNode;
}
if (tail == null) {
head = prev;
} else {
tail.next = prev;
}
}
public class ReverseList<T> extends LinkedList<T>{
public void reverseFrom(int idx) {
if (idx < 0 || idx > size()) {
return;
}
Stack<T> stack = new Stack<T>();
while (idx < size()) {
stack.push(remove(idx));
}
while (!stack.isEmpty()) {
add(stack.pop());
}
}
/******* Alternative slution ***************/
public void reverseFrom(int idx) {
if (idx < 0 || idx > size()) {
return;
}
reverseFromInternal(idx, size()-1);
}
private void reverseFromInternal(int a, int b) {
if (a >= b) {
return;
}
T far = remove(b);
T near = remove(a);
add(a, far);
add(b, near);
reverseFromInternal(a + 1, b - 1);
}
/****************************************/
public static void main(String [] args) {
ReverseList<String> list = new ReverseList<>();
for (int i = 0; i < 10; i++) {
list.add(Integer.toString(i));
}
list.reverseFrom(5);
System.out.println(list);
}
}
public void reverseFrom(int index) {
if (index == 0) {
reverse();
return;
}
Node tmp = head;
Node previous = null;
for (int i = 0; i < index; i++) {
previous = tmp;
tmp = tmp.next;
}
Node newHead = tmp;
LinkedList subLinkedList = new LinkedList();
subLinkedList.head = newHead;
subLinkedList.reverse();
previous.next = subLinkedList.head;
}

Generic linked-list remove, size, get methods

I have just seen this wonderful code from this question "Generic Linked List in java" here on Stackoverflow. I was wandering on how do you implement a method remove (to remove a single node from the linkedlist), size (to get the size of list) and get (to get the a node). Could someone please show me how to do it?
public class LinkedList<E> {
private Node head = null;
private class Node {
E value;
Node next;
// Node constructor links the node as a new head
Node(E value) {
this.value = value;
this.next = head;//Getting error here
head = this;//Getting error here
}
}
public void add(E e) {
new Node(e);
}
public void dump() {
for (Node n = head; n != null; n = n.next)
System.out.print(n.value + " ");
}
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.add("world");
list.add("Hello");
list.dump();
}
}
Your implementation of LinkedList for operation remove(), size() and contains()
looks like this:
static class LinkedList<Value extends Comparable> {
private Node head = null;
private int size;
private class Node {
Value val;
Node next;
Node(Value val) {
this.val = val;
}
}
public void add(Value val) {
Node oldHead = head;
head = new Node(val);
head.next = oldHead;
size++;
}
public void dump() {
for (Node n = head; n != null; n = n.next)
System.out.print(n.val + " ");
System.out.println();
}
public int size() {
return size;
}
public boolean contains(Value val) {
for (Node n = head; n != null; n = n.next)
if (n.val.compareTo(val) == 0)
return true;
return false;
}
public void remove(Value val) {
if (head == null) return;
if (head.val.compareTo(val) == 0) {
head = head.next;
size--;
return;
}
Node current = head;
Node prev = head;
while (current != null) {
if (current.val.compareTo(val) == 0) {
prev.next = current.next;
size--;
break;
}
prev = current;
current = current.next;
}
}
}

Passing elements from an array to a linked list

I was trying to make an algorithm that takes some elements into a Linked List and after it passes those elements into an array using Bubble Sort to sort the elements. I sorted those elements but now I'm not able to pass the elements from the array to the Linked List again.
I used the method passIntoArray(Node head, int arr[]) to take the elements from the Linked List and pass into the array. The method count(Node head) is used to check how many elements there were in the Linked List(Currently being used as index of the array). Could anyone help me?
package LinkedList;
public class LinkedListSorting {
private static int count(Node head) {
if (head == null) return 0;
Node current = head;
int count = 0;
while(current != null) {
count++;
current = current.next;
} return count;
}
private static void passIntoArray(Node head, int arr[]) {
if (head == null) return;
Node current = head;
int i = 0;
while(current != null) {
arr[i] = current.data;
i++;
current = current.next;
}
int length = count(head);
for(i=0; i<length; i++) {
for(int j=i+1; j<length; j++) {
if(arr[i]>arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(i=0; i<length; i++) {
System.out.println(arr[i]);
}
}
//FROM THE ARRAY TO THE LL
private static void display(Node head) {
if (head == null) return;
Node current = head;
while(current != null) {
System.out.print(current.data + " --> ");
current = current.next;
} System.out.println(current);
}
private static class Node{
private int data;
private Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public static void main(String args[]) {
Node head = new Node(5);
Node first = new Node(2);
Node second = new Node(3);
Node third = new Node(4);
Node fourth = new Node(1);
head.next = first;
first.next = second;
second.next = third;
third.next = fourth;
display(head);
System.out.println();
int arr[] = new int[count(head)];
passIntoArray(head, arr);
System.out.println();
}
}
I guess you want to sort the elements of the linked list
It would be better if you sort them in the linked list itself.
public class Main {
private static int count(Node head) {
if (head == null) return 0;
Node current = head;
int count = 0;
while(current != null) {
count++;
current = current.next;
} return count;
}
private static void sortLL(Node head) {
if (head == null) return;
Node current = head;
Node i,j;
int length = count(head);
for(i=head; i!=null; i=i.next) {
for(j=i.next; j!=null; j=j.next) {
if(i.data>j.data) {
int temp = i.data;
i.data = j.data;
j.data = temp;
}
}
}
}
private static void display(Node head) {
if (head == null) return;
Node current = head;
while(current != null) {
System.out.print(current.data + " --> ");
current = current.next;
} System.out.println(current);
}
private static class Node{
private int data;
private Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public static void main(String args[]) {
Node head = new Node(5);
Node first = new Node(2);
Node second = new Node(3);
Node third = new Node(4);
Node fourth = new Node(1);
head.next = first;
first.next = second;
second.next = third;
third.next = fourth;
sortLL(head);
display(head);
}
}

Generic Doubly Linked List Implementation

So I have this basic generic implantation of generic doubly linked list. I have created an insert method which is going to add a node according to the order.
public class DoublyLL <T extends Comparable<T>> {
DNode<T> head;
DNode<T> tail;
public void insertInOrder(T item) { //To create an "ordered" linked list
DNode <T> n = new DNode<>();
if (head == null) {
head = n;
n.data = item;
}
else {
DNode<T> temp = head;
while (temp != null && n.data.compareTo(temp.data) > 0) { // line 18
temp = temp.next;
}
if (temp == head) { //inserting node in first
head.prev = n;
n.next = head;
head = n;
n.data = item;
}
else if (temp == null) { // inserting at last
tail.next = n;
n.prev = tail;
tail = n;
n.data = item;
}
else { //inserting in middle
n.prev = temp.prev;
n.next = temp;
temp.prev.next = n;
temp.prev = n;
n.data = item;
}
}
}
#Override
public String toString() {
DNode temp = head;
String str = "";
while (temp != null) {
str += temp.data + " ";
temp = temp.next;
}
return str;
}
public static void main(String[] args) {
DoublyLL<Integer> list = new DoublyLL<>();
list.insertInOrder(2);
list.insertInOrder(222); //line 62
list.insertInOrder(22222);
System.out.println(list);
}
}
class DNode<T> {
T data;
DNode prev;
DNode next;
}
However, when I'm running this I'm getting the NullPointerException at line 18 and 62. What can I do to get rid of that to make the ordered list like, "2, 22, 2222?
It's hard to say what's the problem without stack trace but it looks like instead of
if (head == null) {
head = n;
n.data = item;
}
you should have
if (head == null) {
head = n;
tail = n;
n.data = item;
}
Otherwise your tail remains null.

Categories