LinkedList Flashcards

1
Q

How to import and initialize a Linked List

A

import java.util.LinkedList;

LinkedList<DataType> Name = new LinkedList<>();</DataType>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

O(n)

A

Access, Search, delete value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

O(1)

A

Insert at head and tail

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to add to head/tail and remove from head/tail

A

addFirst(E e)
addLast(E e)
removeFirst()
removeLast()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to get an element at linked list index

A

linkedList.get(i);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What benefits are in a linked list as opposed to an array?

A

A linked list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Show me how to create the node class

A

You need the constructor

private static class Node(){
int data;
Node next;

Node(int data){
    this.data = data;
    this.next = null;

}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What must you always include so that all other methods can access in a linked list?

A

The head node

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why is the node class private and static?

A

Private so nothing else can mess with our constructor
Static so that it is independent of the linked list’s object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Show me code to insert a node at the end

A

public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Show me how to insert a node at the beginning

A

public void insertAtBeginning(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Show me how to insert at a index

A

// Insert at a specific position
public void insertAt(int index, int data) {
if (index < 0) return;
Node newNode = new Node(data);
if (index == 0) {
newNode.next = head;
head = newNode;
return;
}
Node current = head;
for (int i = 0; current != null && i < index - 1; i++) {
current = current.next;
}
if (current == null) return;
newNode.next = current.next;
current.next = newNode;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Show me how to delete at an index

A

// Delete at a specific position
public void deleteAt(int index) {
if (head == null || index < 0) return;
if (index == 0) {
head = head.next;
return;
}
Node current = head;
for (int i = 0; current.next != null && i < index - 1; i++) {
current = current.next;
}
if (current.next == null) return;
current.next = current.next.next;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Show me how to delete a node by data

A

// Delete a node by value
public void delete(int data) {
if (head == null) return;
if (head.data == data) {
head = head.next;
return;
}
Node current = head;
while (current.next != null && current.next.data != data) {
current = current.next;
}
if (current.next != null) {
current.next = current.next.next;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Show me how to search for a data

A

// Search for an element
public boolean search(int data) {
Node current = head;
while (current != null) {
if (current.data == data) return true;
current = current.next;
}
return false;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Show me how to find length of a list

A

// Find the length of the list
public int size() {
int count = 0;
Node current = head;
while (current != null) {
count++;
current = current.next;
}
return count;
}

17
Q

Show me how to reverse a list

A

// Reverse the linked list
public void reverse() {
Node prev = null, current = head, next;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}

18
Q

Show me how to display an entire node

A

// Display the linked list
public void display() {
Node current = head;
while (current != null) {
System.out.print(current.data + “ -> “);
current = current.next;
}
System.out.println(“null”);
}

19
Q

What is the pattern to iterate through a linked list?

A
  1. Create a new node and set it to head
  2. A while loop that while newNode.next != null
  3. newNode = newNode.next
  4. Decide if you want to delete or add it
20
Q

What is the pattern to add a node

A

currentNode.next = currentNode

21
Q

What is the pattern to delete a node

A

currentNode.next = currentNode.next.next