0% found this document useful (0 votes)
1 views

Linear Data Structures_ Linked Lists Cheatsheet _ Codecademy

The document provides an overview of a LinkedList class in Java, detailing its attributes and methods for adding, removing, and printing nodes. It explains the structure of linked lists, including the head node and how nodes are linked using pointers. Additionally, it covers the process of adding and removing nodes from both the head and tail of the list.

Uploaded by

usernxt123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Linear Data Structures_ Linked Lists Cheatsheet _ Codecademy

The document provides an overview of a LinkedList class in Java, detailing its attributes and methods for adding, removing, and printing nodes. It explains the structure of linked lists, including the head node and how nodes are linked using pointers. Additionally, it covers the process of adding and removing nodes from both the head and tail of the list.

Uploaded by

usernxt123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Cheatsheets / Linear Data Structures

Linked Lists
Linked List Overview

A LinkedList (singly-linked list) class in Java has public class LinkedList {


the following attributes:
a public head Node instance variable
a constructor with a head property public Node head;
.addToHead() to add new nodes to the
head public LinkedList() {
.addToTail() to add new nodes to the
this.head = null;
tail
.removeHead() to remove the head }
node
.printList() to output a human-
public void addToHead(String data) {
readable ordered list of node data values
Node newHead = new Node(data);
Node currentHead = this.head;
this.head = newHead;
if (currentHead != null) {
this.head.setNextNode(currentHead);
}
}

public void addToTail(String data) {


Node tail = this.head;
if (tail == null) {
this.head = new Node(data);
} else {
while (tail.getNextNode() != null)
{
tail = tail.getNextNode();
}
tail.setNextNode(new Node(data));
}
}

public String removeHead() {


Node removedHead = this.head;
if (removedHead == null) {
return null;
}
this.head =
removedHead.getNextNode();
return removedHead.data;
}
public String printList() {
String output = "<head> ";
Node currentNode = this.head;
while (currentNode != null) {
output += currentNode.data + " ";
currentNode =
currentNode.getNextNode();
}
output += "<tail>";
System.out.println(output);
return output;
}

Linked List Constructor

A Java LinkedList constructor takes no public LinkedList() {


parameters and sets the head instance variable to
this.head = null;
null .
}

Adding to the Head

A Java LinkedList class can implement a public public void addToHead(String data) {
void .addToHead() instance method for adding
Node newHead = new Node(data);
new data to the head of the list. .addToHead()
takes a single String data argument. It uses Node currentHead = this.head;
data to create a new Node which it adds to the this.head = newHead;
head of the list. if (currentHead != null) {
this.head.setNextNode(currentHead);
}
}
Adding to the Tail

A Java LinkedList class can implement a public public void addToTail(String data) {
void .addToTail() instance method for adding
Node tail = this.head;
new data to the tail of the list. .addToTail()
takes a single String data argument. It uses if (tail == null) {
data to create a new Node which it adds to the this.head = new Node(data);
tail of the list. } else {
while (tail.getNextNode() != null) {
tail = tail.getNextNode();
}
tail.setNextNode(new Node(data));
}
}

Removing the Head

A Java LinkedList class can implement a public public String removeHead() {


String .removeHead() instance method for
Node removedHead = this.head;
removing the head of the list. .removeHead()
takes no arguments. It removes and returns the head of if (removedHead == null) {
the list’s data , and sets the head’s next node as the return null;
new head. }
this.head = removedHead.getNextNode();
return removedHead.data;
}

Printing the List

A Java LinkedList class can implement a public public String printList() {


String .printList() instance method for
String output = "<head> ";
printing the list in a clear and readable way and takes
no arguments. It iterates through the list and adds the Node currentNode = this.head;
data from each element to a string, which it prints while (currentNode != null) {
and returns at the end of the method. output += currentNode.data + " ";
currentNode =
currentNode.getNextNode();
}
output += "<tail>";
System.out.println(output);
return output;
}
Removing a node from the middle of a linked list

When removing a node from the middle of a linked list,


it is necessary to adjust the link on the previous node
so that it points to the following node. In the given
illustration, the node a1 must point to the node a3
if the node a2 is removed from the linked list.

Linked List data structure

A linked list is a linear data structure where elements


are not stored at contiguous location. Instead the
elements are linked using pointers.
In a linked list data is stored in nodes and each node is
linked to the next and, optionally, to the previous. Each
node in a list consists of the following parts:
1. data
2. A pointer (Or reference) to the next node
3. Optionally, a pointer to the previous node

Adding a new head node in a linked list

When adding a new node to the start of a linked list, it is


necessary to maintain the list by giving the new head
node a link to the current head node. For instance, to
add a new node a0 to the begining of the linked list,
a0 should point to a1 .
The Head Node in Linked Lists

The first node in a linked list is called the head node. If


the linked list is empty, then the value of the head node
is NULL.

Implementing a linked list

A linked list exposes the ability to traverse the list from


one node to another node. The starting node is
considered the head node from where the list can be
traversed.

Print Share

You might also like