0% found this document useful (0 votes)
37 views50 pages

Linked List Lecture II

The document discusses various problems and approaches related to linked lists, including finding the n-th last element, the middle element, and detecting cycles using Floyd's algorithm. It presents two main techniques: counting nodes and using two pointers for the n-th last element, and fast and slow pointers for finding the middle element. Additionally, it highlights common pitfalls in linked list operations and provides practice questions and resources for further learning.

Uploaded by

remidan37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views50 pages

Linked List Lecture II

The document discusses various problems and approaches related to linked lists, including finding the n-th last element, the middle element, and detecting cycles using Floyd's algorithm. It presents two main techniques: counting nodes and using two pointers for the n-th last element, and fast and slow pointers for finding the middle element. Additionally, it highlights common pitfalls in linked list operations and provides practice questions and resources for further learning.

Uploaded by

remidan37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Linked List II

1 2 3 4
Pre-requisites
1. Linked list
Problem I
Return the n-th last element of a singly linked list.

3
Approach I

● Count number of nodes (K) in one pass


● Find (K - n)th node on second pass

4
Two Pointer Technique in Linked List

5
Approach II

● iterate with two pointers


● one seeking the tail and the other lagging by the nth amount.

1 2 3 4 5

6
Simulation

1 2 3 4 5

7
Simulation

1 2 3 4 5

8
Simulation

1 2 3 4 5

9
Simulation

1 2 3 4 5

10
Simulation

1 2 3 4 5

11
Simulation

1 2 3 4 5

12
Simulation

1 2 3 4 5

Nth element from right

13
Problem II
Return the middle element of a linked List

14
Approach : Fast and slow Pointers

● Iterate with two pointers: Fast and slow


● Slow pointer goes one step at a time
● Fast goes two steps at a time
● When the fast pointer moves to the very end of the Linked List, the
slow pointer is going to point to the middle element of the linked
list.

15
Simulation

16
17
The middle
node

18
Pair Programming
Middle element of a linked list

19
Check Point
Link

20
Floyd’s Cycle Finding Algorithm

21
Initial Problem
Given a linked list, return the node where the cycle begins?

22
Floyd's algorithm consists of two phases and uses two
pointers, usually called tortoise and rabbit.

23
Phase I - Cycle Detection

● Rabbit = cur.next.next goes twice as fast as tortoise = cur.next


● Since tortoise is moving slower, the rabbit catches up to the
tortoise at some intersection point
● Note that the intersection point is not the cycle entrance in the
general case.

24
25
Phase 1

26
Phase 1

27
Phase 1

28
Phase 1

29
Phase 1

30
Phase 1

31
Phase 1

32
Phase II - Determine Cycle Entrance
To compute the cycle entrance, let's note that the rabbit has traversed
twice as many nodes as the tortoise, i.e.

2•dtortoise = drabbit, implying:

2•(F + m•C + a) = F + n•C + a, where n, m is some positive


integer.

Hence the coordinate of the intersection point is F = (n-2•m)•C - a

33
● we give the tortoise a second chance by slowing down the
rabbit,
○ tortoise = tortoise.next
○ rabbit = rabbit.next
● The tortoise is back at the starting position, and the rabbit
starts from the first intersection point.
Phase 2

35
Phase 2 is over!

Rabbit

36
Back to Initial Problem
Pair Programming
Linked List Cycle

37
Common Pi alls
Can you spot the error?

def containsTarget(head, target):


while head.val != target:
head = head.next

return head.val == target


Null Pointer Example

def containsTarget(head, target):


while head.val != target:
head = head.next

What if head
return head.val == target is null?
Solution

def containsTarget(head, target):


while head and head.val != target:
head = head.next

return head.val == target


Can you spot the error?

def middleNode(self, head):


slow = head
fast = head

while fast:
slow = slow.next
fast = fast.next.next
return slow
Solution

def middleNode(self, head):


slow = head
fast = head

while fast and fast.next:


slow = slow.next
fast = fast.next.next
return slow
● Null pointer
○ head.next <> check existence of head
○ head.next.next <> check existence of head and head.next
Pitfalls
def deleteNodeAtGivenPosition(position,head):
if head is None:
return
index = 0
previous = None
while head.next and index < position:
previous = head
head = head.next
index += 1
previous.next = head.next
return head
Losing the reference to head of the linked list
Example
def deleteNodeAtGivenPosition(position,head):
if head is None:
return
index = 0 What if head
previous = None is null?
while head.next and index < position:
previous = head Is head the head of
head = head.next the modified linked
index += 1 list?
previous.next = head.next
return head
Pitfalls - Continued

● Losing the reference to head of the linked list


○ Put the head in a variable before any operation to return the
head at the end

47
Practice Questions
● Reverse Linked List

● Palindrome Linked List

● Merge Two Sorted Lists

● Remove duplicates from sorted list

● Partition List

● Linked List Cycle II

● LRU Cache

● Delete Node in a Linked List

48
Resources
● Leetcode Explore Card: has excellent track path with good explanations
● Leetcode Solution (Find the Duplicate Number) : has good explanation
about Floyd’s cycle detection algorithm with good simulation
● Elements of Programming Interview book: has a very good Linked List
Problems set

49
50

You might also like