Tags » Linked List

[Leetcode] Linked List Random Node

Description:

Given a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen. 463 more words

Python

Hackerrank : Cycle Detection

/*
*  Code By : Pritish Thakkar
*  Path :  All Domains > Data Structures > Linked Lists 
*  Tags : Pointers + Linked List + Implementation
*  Funny Logic : ** Must see editorial after this **
*   The logic below works fine because of poor test cases ! 73 more words
Data Structures

Hackerrank : Delete duplicate-value nodes from a sorted linked list

/*
*  Code By : Pritish Thakkar
*  Path :  All Domains > Data Structures > Linked Lists 
*  Tags : Pointers + Linked List + Implementation
*/

Node* RemoveDuplicates(Node *head)
{ 
    Node *temp = head;
    while(temp != NULL){
        Node *nn = temp;
        while(nn->next!=NULL && temp->data == nn->next->data){
            nn->next = nn->next->next;
        }
        if(nn->data != temp->data) temp = nn;
        else temp = temp->next;
    }
    return head;
}

Data Structures

Hackerrank : Get Node Value

/*
*  Code By : Pritish Thakkar
*  Path :  All Domains > Data Structures > Linked Lists 
*  Tags : Pointers + Linked List + Implementation
*/

int GetNode(Node *head,int positionFromTail)
{ 
    Node *temp = head;
    int size=0;
    while(temp != NULL) {
        ++size;
        temp = temp->next;
    }
    temp = head;
    for(int i=0;i<size-positionFromTail-1;i++){
        temp = temp->next;
    }
    return temp->data;
    
}
Data Structures

Custom LinkedList

/**
 * 
 */
package org.rash.collections;

/**
 * @author Ammi
 */
public class CustomLinkedList {
	private int counter;
	private Node head;

	public void addFirst(T data) {
		Node newNode = new Node(data);
		if (head == null) {
			head = newNode;
		} else {
			newNode.next = head;
			head = newNode;
		}
		incrementCounter();
	}

	public void addLast(T data) {
		Node newNode = new Node(data);
		if (head == null) {
			head = newNode;
		} else {
			Node currentNode = head;
			for (; currentNode.next != null; currentNode = currentNode.next)
				;
			newNode.next = currentNode.next;
			currentNode.next = newNode;
		}
		incrementCounter();
	}

	public void add(int index, T data) {
		Node newNode = new Node(data);
		if (index < 0 || index > counter) {
			return;
		}
		Node currentNode = head;
		Node previousNode = null;
		for (int i = 0; i < counter && i < index
				&& currentNode.next != null; i++, previousNode = currentNode, currentNode = currentNode.next)
			;
		newNode.next = previousNode.next;
		previousNode.next = newNode;
		incrementCounter();
	}

	public T getFirst() {
		if (head == null) {
			return null;
		} else {
			return head.data;
		}
	}

	public T getLast() {
		if (head == null) {
			return null;
		} else {
			Node currentNode = head;
			for (; currentNode.next != null; currentNode = currentNode.next)
				;
			return currentNode.data;
		}
	}

	public Node get(int index) {
		if (head == null) {
			return null;
		} else {
			if (index < 0 || index > counter) {
				return null;
			}
			Node currentNode = head;
			for (int i = 0; i < index && currentNode.next != null; i++, currentNode = currentNode.next)
				;
			return currentNode;
		}
	}

	public boolean removeFirst() {
		if (head == null) {
			return false;
		} else {
			head = head.next;
			decrementCounter();
			return true;
		}
	}

	public boolean removeLast() {
		if (head == null) {
			return false;
		} else {
			Node previousNode = null;
			for (Node currentNode = head; currentNode.next != null; previousNode = currentNode, currentNode = currentNode.next)
				;
			previousNode.next = null;
			decrementCounter();
			return true;
		}
	}

	public boolean remove(int index) {
		if (head == null) {
			return false;
		} else {
			if (index < 0 || index > counter) {
				return false;
			}
			Node currentNode = head;
			Node previousNode = null;

			for (int i = 0; i < index; i++, previousNode = currentNode, currentNode = currentNode.next)
				;
			previousNode.setNext(currentNode.getNext());
			decrementCounter();
			return true;
		}

	}

	public boolean isEmpty() {
		if (head == null)
			return true;
		return false;
	}

	public int size() {
		return counter;
	}

	public void incrementCounter() {
		counter++;
	}

	public void decrementCounter() {
		this.counter--;
	}

	/**
	 * @return the counter
	 */
	public int getCounter() {
		return this.counter;
	}

	/**
	 * @param counter
	 *            the counter to set
	 */
	public void setCounter(int counter) {
		this.counter = counter;
	}

	public Node reverseLinkedList(Node head) {
		if (head == null) {
			return null;
		} else {
			Node previousNode = null;
			for (Node currentNode = head, nextNode = null; currentNode.next != null; previousNode = currentNode, currentNode = nextNode) {
				nextNode = currentNode.next;
				currentNode.next = previousNode;
			}
			return previousNode;
		}
	}

	public Node findMiddleElement(Node head) {
		Node slowNode = head;
		for (Node fastNode = head; fastNode.next != null; slowNode = slowNode.next, fastNode = slowNode.next.next)
			;
		int middleIndex = counter / 2;
		Node middleNode = get(middleIndex);
		return slowNode;
	}

	public boolean checkPalindrome(Node head) {
		Node middleElement = findMiddleElement(head);
		Node secondHalfHead = middleElement.next;
		Node reverseLinkedList = reverseLinkedList(secondHalfHead);
		while (head != null && reverseLinkedList != null) {
			if (head.data == reverseLinkedList.data) {
				head = head.next;
				reverseLinkedList = reverseLinkedList.next;
				continue;
			} else {
				return false;
			}
		}
		return true;
	}

	public T findNthFromLastNode(int n) {
		if (head == null) {
			return null;
		}
		if (n < 0 || n > counter) {
			return null;
		}
		Node currentNode = head;
		for (int i = 0; currentNode.next != null && i < counter - n; i++, currentNode = currentNode.next)
			;

		return currentNode.data;
	}

	public void display() {
		for (Node currentNode = head; currentNode != null; currentNode = currentNode.next) {
			System.out.print(currentNode.data + "\t");
		}
	}

	public static void main(String[] args) {
		CustomLinkedList t = new CustomLinkedList();
		t.addLast(4);
		t.addLast(2);
		t.add(1, 3);
		t.addLast(5);
		t.addFirst(8);
		t.display();

		System.out.println("\n" + t.findNthFromLastNode(3));
	}

}

class Node {
	T data;
	Node next;

	/**
	 * @param data
	 */
	public Node(T data) {
		super();
		this.data = data;
	}

	/**
	 * @return the data
	 */
	public T getData() {
		return data;
	}

	/**
	 * @param data
	 *            the data to set
	 */
	public void setData(T data) {
		this.data = data;
	}

	/**
	 * @return the next
	 */
	public Node getNext() {
		return next;
	}

	/**
	 * @param next
	 *            the next to set
	 */
	public void setNext(Node next) {
		this.next = next;
	}
}

Java

When to use LinkedList over ArrayList?

Yeah, I know, this is an ancient question, but is so common  among beginning developers that I think it deserves a blog post. 158 more words

Lessons

Linked lists

Linked List is a linear data structure and it is very common data structure which consists of group of nodes in a sequence which is divided in two parts. 249 more words

Linked List