Linked List Flashcards

(14 cards)

1
Q

Make a Linked list

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

Calculate sum of a linked List

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

Add node to a linked list

A

def add_node(prev_node, node_to_add):
node_to_add.next= prev_node.next
prev_node.next= node_to_add

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

Delete a node from linked list

A

def delete_node(prev_node):
prev_node.next=prev_node.next.next

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

(1) Make a doubly linked list (2) add to end (3) remove from end (4) add to start (5) remove from start

A

class ListNode:
def __init__(self, val):
self.val = val
self.next = None
self.prev = None

def add_to_end(node_to_add):
node_to_add.next = tail
node_to_add.prev = tail.prev
tail.prev.next = node_to_add
tail.prev = node_to_add

def remove_from_end():
if head.next == tail:
return

node_to_remove = tail.prev
node_to_remove.prev.next = tail
tail.prev = node_to_remove.prev

def add_to_start(node_to_add):
node_to_add.prev = head
node_to_add.next = head.next
head.next.prev = node_to_add
head.next = node_to_add

def remove_from_start():
if head.next == tail:
return

node_to_remove = head.next
node_to_remove.next.prev = head
head.next = node_to_remove.next

head = ListNode(None)
tail = ListNode(None)
head.next = tail
tail.prev = head

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

(1) Make a doubly linked list (2) add node (3) delete node

A

class ListNode:
def __init__(self, val):
self.val = val
self.next = None
self.prev = None

Let node be the node at position i
def add_node(node, node_to_add):
prev_node = node.prev
node_to_add.next = node
node_to_add.prev = prev_node
prev_node.next = node_to_add
node.prev = node_to_add

Let node be the node at position i
def delete_node(node):
prev_node = node.prev
next_node = node.next
prev_node.next = next_node
next_node.prev = prev_node

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

Example 1: Given the head of a linked list with an odd number of nodes head, return the value of the node in the middle.

For example, given a linked list that represents 1 -> 2 -> 3 -> 4 -> 5, return 3.

A

def get_middle(head):
length = 0
dummy = head
while dummy:
length += 1
dummy = dummy.next

for _ in range(length // 2):
    head = head.next

return head.val

OR The Smart way to add by Fast and Slow pointers

def get_middle(head):
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next

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

Given the head of a linked list, determine if the linked list has a cycle.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer.

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

Given the head of a linked list and an integer k, return the
k-th node from the end.

For example, given the linked list that represents 1 -> 2 -> 3 -> 4 -> 5 and k = 2, return the node with value 4, as it is the 2nd node from the end.

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

Given the head of a singly linked list, return the middle node of the linked list.

If there are two middle nodes, return the second middle node.

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

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

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

Reverse a linked list

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

Example: 24. Swap Nodes in Pairs

Given the head of a linked list, swap every pair of nodes. For example, given a linked list 1 -> 2 -> 3 -> 4 -> 5 -> 6, return a linked list 2 -> 1 -> 4 -> 3 -> 6 -> 5.

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

Reverse Linked List II

Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.

A

Definition for singly-linked list.

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