Skip to content

Commit

Permalink
Practice some linkedlist questions
Browse files Browse the repository at this point in the history
  • Loading branch information
sangaryousmane committed Feb 26, 2024
1 parent 015dc61 commit 757d054
Showing 1 changed file with 99 additions and 105 deletions.
204 changes: 99 additions & 105 deletions src/advance/linkedlist/Play.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,156 +2,150 @@

public class Play {

private Node head;
private Node tail;
private class ListNode {
private ListNode next;
private int value;

private class Node {
private int data;
private Node next;
private Node prev;

public Node(int data) {
this.data = data;
}

public Node(int data, Node next) {
this.data = data;
this.next = next;
public ListNode(int value, ListNode node) {
this.value = value;
this.next = node;
}

public Node() {
public ListNode(int value) {
this.value = value;
}

@Override
public String toString() {
return "Node: " + data;
return "value = " + value;
}
}

public Play() {
}
private ListNode head;

// Insert first element
public void insertFirstDll(int value) {
Node newNode = new Node(value);
public void addFirst(int value) {
ListNode newNode = new ListNode(value);
newNode.next = head;
newNode.prev = null;

if (head != null)
head.prev = newNode;

head = newNode;
}

public void displayDll() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " <-> ");
temp = temp.next;
}
System.out.println("END");
}

// Add to the end of the list
public void insertLastDll(int value) {
Node newNode = new Node(value);
Node lastNode = head;
public void addLast(int value) {
ListNode newNode = new ListNode(value);
newNode.next = null;

if (head == null) {
newNode.prev = null;
head = newNode;
return;
}

while (lastNode.next != null) {
lastNode = lastNode.next;
ListNode temp = head;
while (temp.next != null) {
temp = temp.next;
}
lastNode.next = newNode;
newNode.prev = lastNode;
temp.next = newNode;
}

// Get element at an index
public Node getIndex(int index) {
Node temp = head;

// Get node given index
public ListNode getNode(int index) {
ListNode temp = head;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
return temp;
}

// Find a value in a list
public Node findNodeByValue(int value) {
Node temp = head;
while (temp != null) {
if (temp.data == value)
return temp;
temp = temp.next;
// Find node given the value
public ListNode findNode(int data) {
ListNode node = head;
while (node != null) {
if (node.value == data)
return node;
node = node.next;
}
return null;
}

// check if the list is empty
public boolean isEmpty() {
return head == null;
}

// get the size of the list
public int sizeDll() {
Node temp = head;
int size = 0;
public int getLength() {
ListNode temp = head;
int length = 0;
while (temp != null) {
size++;
length++;
temp = temp.next;
}
return size;
return length;
}

public void insertAtIndex(int after, int value) {
Node prevNode = findNodeByValue(after);
if (prevNode == null) {
System.out.println("Node not found");
return;
}

Node newNode = new Node(value);
newNode.next = prevNode.next;
prevNode.next = newNode;
newNode.prev = prevNode;
if (newNode.next != null) {
newNode.next.prev = newNode;
}
}

// Return -1 if the head is null
public int deleteFirst() {
if (head == null)
return -1;
if (head == null) return -1;

int data = head.data;
int data = head.value;
head = head.next;
head.prev = null;
return data;
}

public int deleteLastNode(){
// data
// find lastnode
// move the lastnode back
// set the next of the lastnode to null
if(head == null){
return -1;
}
if (head.next == null){
int data = head.data;
head = null;
return data;
public int deleteLast(){
if (head == null) return -1;
// if (head.next == null){
// deleteFirst();
// return head.value;
// }
ListNode last=head;
int size = 1;
while (last.next != null){
size++;
last = last.next;
}
Node lastNode = head;
while (lastNode.next != null){
lastNode = lastNode.next;
ListNode secondToLast=head;
for(int i = 0; i < size - 2; i++){
secondToLast = secondToLast.next;
}
int data = lastNode.data;
lastNode.prev.next = null;
int data= last.value;
secondToLast.next = null;
return data;
}

public boolean detectCycle(){
ListNode turtle=head;
ListNode hare = head;

while (hare != null && hare.next != null){
turtle = turtle.next; // single step
hare = hare.next.next; // Double step
if (turtle == hare)
return true;

}
return false;
}

public void displayNodes() {
ListNode temp = head;

while (temp != null) {
System.out.print(temp.value + " -> ");
temp = temp.next;
}
System.out.println("END");
}

public static void main(String[] args) {
Play p = new Play();
p.addFirst(5);
p.addFirst(4);
p.displayNodes();
p.addLast(15);
// p.addLast(13);
p.displayNodes();
p.deleteFirst();
p.displayNodes();
p.deleteLast();
p.displayNodes();
p.deleteLast();
p.displayNodes();
p.deleteLast();
p.displayNodes();



// System.out.println("The length is: " + p.getLength());
}
}

0 comments on commit 757d054

Please sign in to comment.