Add reverse linked list in java (#182)
parent
2017c59218
commit
756589e272
|
@ -14,6 +14,7 @@
|
|||
1. [Singly Linked List](java/singly.java)
|
||||
2. [Doubly Linked List](java/doubly.java)
|
||||
2. [Circular Linked List](java/circular.java)
|
||||
2. [Reverse Linked List](java/reverse.java)
|
||||
|
||||
### JavaScript
|
||||
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
class LinkedList{
|
||||
class Node{
|
||||
int item;
|
||||
Node next;
|
||||
|
||||
public Node(int item) {
|
||||
this.item = item;
|
||||
}
|
||||
}
|
||||
|
||||
Node head = null;
|
||||
|
||||
public void addNode(int item){
|
||||
Node newElem = new Node(item);
|
||||
|
||||
if(this.head != null){
|
||||
newElem.next = this.head;
|
||||
}
|
||||
|
||||
this.head = newElem;
|
||||
}
|
||||
|
||||
public void reverse(){
|
||||
Node prev = null;
|
||||
Node current = this.head;
|
||||
Node next = null;
|
||||
|
||||
while (current != null) {
|
||||
next = current.next;
|
||||
current.next = prev;
|
||||
prev = current;
|
||||
current = next;
|
||||
}
|
||||
this.head =prev;
|
||||
}
|
||||
|
||||
public void print() {
|
||||
Node current = head;
|
||||
if(head == null) return;
|
||||
System.out.print("[ ");
|
||||
while(current != null) {
|
||||
System.out.print(current.item + " ");
|
||||
current = current.next;
|
||||
}
|
||||
System.out.println("]");
|
||||
}
|
||||
}
|
||||
|
||||
public class reverse{
|
||||
|
||||
public static void main(String [] args){
|
||||
LinkedList list = new LinkedList();
|
||||
|
||||
list.addNode(2);
|
||||
list.addNode(7);
|
||||
list.addNode(1);
|
||||
list.addNode(8);
|
||||
list.addNode(5);
|
||||
|
||||
System.out.print("before : \t");
|
||||
list.print();
|
||||
|
||||
list.reverse();
|
||||
|
||||
System.out.print("after reverse : ");
|
||||
list.print();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
to run the file :
|
||||
javac reverse.java
|
||||
java reverse
|
||||
|
||||
results :
|
||||
before : [ 5 8 1 7 2 ]
|
||||
after reverse : [ 2 7 1 8 5 ]
|
||||
*/
|
Loading…
Reference in New Issue