Linked Lists Flashcards

1
Q

Linked List node class constructor code

A
class Node:
    def \_\_init\_\_(self, value):
        self.value = value
        self.next = None
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Linked List constructor code

A
class LinkedList:
    def \_\_init\_\_(self, value):
        new_node = Node(value)
        self.head = new_node
        self.tail = new_node
        self.length = 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Linked List print list code

A
def print_list(self):
        temp = self.head
        while temp is not None:
            print(temp.value)
            temp = temp.next
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Linked List append code

A
def append(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            self.tail.next = new_node
            self.tail = new_node
        self.length += 1
        return True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Linked List pop code

A
def pop(self):
        if self.length == 0:
            return None
        temp = self.head
        pre = self.head
        while(temp.next):
            pre = temp
            temp = temp.next
        self.tail = pre
        self.tail.next = None
        self.length -= 1
        if self.length == 0:
            self.head = None
            self.tail = None
        return temp
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Linked List prepend code

A
def prepend(self, value):
        new_node = Node(value)
        if self.length == 0:
            self.head = new_node
            self.tail = new_node
        else:
            new_node.next = self.head
            self.head = new_node
        self.length += 1
        return True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Linked List pop_first code

A
def pop_first(self):
        if self.length == 0:
            return None
        temp = self.head
        self.head = self.head.next
        temp.next = None
        self.length -= 1
        if self.length == 0:
            self.tail = None
        return temp
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Linked List get code

A
def get(self, index):
        if index < 0 or index >= self.length:
            return None
        temp = self.head
        for _ in range(index):
            temp = temp.next
        return temp
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Linked List set value code

A
def set_value(self, index, value):
        temp = self.get(index)
        if temp:
            temp.value = value
            return True
        return False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Linked List insert code

A
def insert(self, index, value):
        if index < 0 or index > self.length:
            return False
        if index == 0:
            return self.prepend(value)
        if index == self.length:
            return self.append(value)
        new_node = Node(value)
        temp = self.get(index - 1)
        new_node.next = temp.next
        temp.next = new_node
        self.length += 1   
        return True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Linked List remove code

A
def remove(self, index):
        if index < 0 or index >= self.length:
            return None
        if index == 0:
            return self.pop_first()
        if index == self.length - 1:
            return self.pop()
        pre = self.get(index - 1)
        temp = pre.next
        pre.next = temp.next
        temp.next = None
        self.length -= 1
        return temp
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Linked List reverse code

A
def reverse(self):
        temp = self.head
        self.head = self.tail
        self.tail = temp
        after = temp.next
        before = None
        for _ in range(self.length):
            after = temp.next
            temp.next = before
            before = temp
            temp = after
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Removing an item from the tail end of a Linked List is which Big O?

A

O(n)

Because you have to start at the beginning of the Linked List an iterate through to the end

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

Removing an item from the beginning of a Linked List is which Big O?

A

O(1)

This is a place where Linked Lists are better than Lists. Lists are O(n) when removing the first item because of the reindexing that is required.

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

Finding an item by index in a Linked List is which Big O?

A

O(n)

You have to iterate through the Linked List until you get to the index you are looking for.

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

Linked List Big O of Append?

A

O(1)

17
Q

Linked List Big O of Pop?

A

O(n)

18
Q

Linked List Big O of Prepend?

A

O(1)

19
Q

Linked List Big O of Pop (first)?

A

O(1)

20
Q

Linked List Big O of Insert

A

O(n)

21
Q

Linked List Big O of Remove

A

O(n)

22
Q

Linked List Big O of Lookup by Index

A

O(n)

23
Q

Linked List Big O of Lookup by Value

A

O(n)

24
Q

Two attributes of a Linked List Node class?

A

self. value = value

self. next = next