Discussion Week 4, Schemes and Mind Maps of Computer science

Problem: Reverse Linked List. Leetcode questions #206. // One possible solution. Node* reverseList(struct ListNode* head).

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 03/01/2023

christin
christin 🇺🇸

4.6

(18)

263 documents

1 / 43

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS32: Introduction to Computer Science II
Discussion Week 4
Yichao (Joey)
Jan. 31, 2019
Credit: Carey Nachenberg, Junheng Hao
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download Discussion Week 4 and more Schemes and Mind Maps Computer science in PDF only on Docsity!

CS32: Introduction to Computer Science II

Discussion Week 4

Yichao (Joey)

Jan. 31, 2019

Credit: Carey Nachenberg, Junheng Hao

Announcements

● Homework 2 is due on 11PM Tuesday, February 4

● (from your LA, Matthew) Please fill out this feedback form for me. I know

we’ve only had one discussion together so far, but it will help a lot!

https://tinyurl.com/StudentsToLAsW

● Minimum Requirement

○ Key component as unit: Node (with value and pointer to next node) ○ Head pointer → points to the first term

● Regular operations

○ Insertion ○ Search ○ Removal

● Pros and cons

○ Efficient insertion, flexible memory allocation, simple implementation ○ High complexity of search

Linked List: Review

Basis

typedef int ItemType; Struct Node { ItemType value; Node *next; };

● Example: Insert as head in a list

● Steps

a) Create a new node and call the pointer p b) Make its next pointer point to the first item c) Make the head pointer to the new node

Linked List

Insertion: Add a new node to a list

//Skeleton: Linked list insertion //=====================================

//insert as head p->next = head; Head = p;

//insert after end: End node: q q->next = p; p->next = nullptr;

//insert in the middle: node q p->next = q->next; q->next = p;

head

new

p

p head

p

head

Linked List

Removal

● Remember to set the previous node q’s

next pointer to point the next node of p

q->next = p->next; delete p ● What if p == head? What if p prints to the last node in the linked list?

// Skeleton Code: Linked list removal // =====================================

void remove(int valToRemove, Node* head) { Node *p = head, *q = NULL; while (p != NULL) { if (p->value == valToRemove) break; q = p; p = p->next;} if (p == NULL) return; if ( p == head ) //special case head = p->next; else q->next = p->next; delete p; }

q p

● Pros:

○ Efficient insertion (add new data items)

○ Flexible memory allocation

● Cons:

○ Slow search (search is more important than insertion and removal in real

situations)

■ e.g. retrieve the fifth value of the list.

■ e.g. a list of values is sorted, find 10 in the linked list

● Many variations

○ Doubly linked lists

○ Sorted linked lists

○ Circularly linked lists

Linked List

Conclusion

Double Linked List

Insertion: How many cases to consider?

● Four cases:

○ Insert before the head

○ Insert after the tail

○ Insert somewhere in the middle

○ When list is empty

Double Linked List

Insertion: Before head / After tail

● Steps for insertion before head:

○ Set the prev of head to the new node p ○ Set the next of p to head ○ p becomes the new head ○ head->prev = NULL;

● Steps for insertion after tail:

○ Similar to insertion before head (try it yourself!)

Double Linked List

Insertion to empty list / Search

● Insertion to an empty list

● Search in doubly linked list

○ Similar to standard linked list

○ Can be done either from head or tail

head = tail = p; p->next = p->prev = NULL;

Double Linked List

Removal

● Removal is more complex!

● Consider the following cases:

○ Check if the node p is the head (p == head). Let this boolean be A.

○ Check if the node p is the tail (p == tail). Let this boolean be B.

● Different cases:

○ Case 1 (A, but not B): P is the head of the list and there is more than one node.

○ Case 2 (B, but not A): P is the tail of the list, and there is more than one node.

○ Case 3 (A and B): P is the only node.

○ Case 4 (not A and not B): P is in the middle of the list.

Double Linked List

Copy a doubly linked list (and more)

● Steps

○ Create head and tail for the new list

○ Iterate through the old list. For each node, copy its value to a new node.

○ Insert the new node to the tail of the new list.

○ Repeat until we have iterated the entire old list.

○ Set NULL before head and next of tail.

● Tips for linked list problems

○ To draw diagrams of nodes and pointers will be extremely helpful.

○ When copying a linked list, only copy stored values to new nodes. Do not copy

pointers.

○ You need to check edge cases!

● Linked list where all nodes are connected to form a circle.

○ There is no NULL at the end. ○ Can be a singly circular linked list or doubly circular linked list.

● Pros:

○ Any points can be head (starting point). ○ Implementation for queue. ○ Fit to repeatedly go around the list.

Circular Linked List

Motivation and properties

We can maintain a pointer to the last inserted node and front can always be obtained as next of last.

Problem: Reverse Linked List

Leetcode questions #

Let’s see what happens in these lines of codes! [Link]

Circular vs Linked List with Loop

● Two different tasks:

○ Tell whether the linked list is circular ○ Tell whether there is a loop in the linked list (this is much harder!)