1
00:00:00,000 --> 00:00:04,400
A linked list stores a value and a
reference to the next node in each node

2
00:00:04,480 --> 00:00:05,280
object.

3
00:00:05,600 --> 00:00:08,080
Following those references visits the
sequence.

4
00:00:08,560 --> 00:00:12,880
Our example reverses the references
connecting three existing nodes.

5
00:00:13,120 --> 00:00:17,680
A Python list is a dynamic array, so it is
a different structure despite the shared

6
00:00:17,760 --> 00:00:18,800
word list.

7
00:00:19,040 --> 00:00:24,000
Python has no built-in singly linked list
type; we define a small Node class

8
00:00:24,080 --> 00:00:25,360
for the interview example.

9
00:00:25,667 --> 00:00:28,227
The head refers to the first node.

10
00:00:28,267 --> 00:00:31,907
A node contains its own value and a next
reference.

11
00:00:31,987 --> 00:00:35,627
The end is represented by None rather than
another node.

12
00:00:35,667 --> 00:00:40,107
To reach the third value, start at head
and follow two references.

13
00:00:40,147 --> 00:00:43,627
There is no constant-time numeric index
into this chain.

14
00:00:43,667 --> 00:00:47,987
If you already hold the required node
references, changing a link takes constant

15
00:00:48,067 --> 00:00:48,747
time.

16
00:00:48,787 --> 00:00:52,707
Finding the required position can still
require a linear traversal.

17
00:00:52,933 --> 00:00:57,173
Import dataclass and use it to define the
small Node class.

18
00:00:57,213 --> 00:01:01,813
The generated constructor accepts a value
and an optional next reference.

19
00:01:01,893 --> 00:01:06,093
Each Node holds an integer and either
another Node or None.

20
00:01:06,133 --> 00:01:11,013
Setting equality generation to false
preserves object identity for comparisons

21
00:01:11,093 --> 00:01:12,986
between these mutable nodes.

22
00:01:13,040 --> 00:01:17,120
Two nodes can contain the same integer
while remaining distinct nodes.

23
00:01:17,146 --> 00:01:21,813
The reversal changes next references and
preserves those node identities.

24
00:01:22,033 --> 00:01:26,353
Previous identifies the reversed prefix,
which starts empty.

25
00:01:26,593 --> 00:01:30,993
Current identifies the next node to move
from the untouched suffix.

26
00:01:31,273 --> 00:01:35,740
Following saves current's old next
reference before that link changes.

27
00:01:35,846 --> 00:01:39,313
Without this saved reference, the
assignment can disconnect the rest of the

28
00:01:39,513 --> 00:01:42,460
input from the references the algorithm
retains.

29
00:01:42,486 --> 00:01:46,433
The reversal changes links between
existing nodes; it does not need to

30
00:01:46,513 --> 00:01:49,153
allocate a second chain of node objects.

31
00:01:49,400 --> 00:01:54,360
Before an update, previous is None and
current refers to four, followed

32
00:01:54,440 --> 00:01:56,200
by seven and nine.

33
00:01:56,360 --> 00:01:59,880
After moving four, its next reference
becomes None.

34
00:02:00,120 --> 00:02:03,560
Previous refers to four, and current
refers to seven.

35
00:02:03,720 --> 00:02:07,400
After moving seven, its next reference
points back to four.

36
00:02:07,680 --> 00:02:11,400
Previous now refers to seven and current
refers to nine.

37
00:02:11,480 --> 00:02:16,120
At each stage, the reversed and untouched
chains together contain all three

38
00:02:16,200 --> 00:02:17,400
original nodes.

39
00:02:17,700 --> 00:02:22,500
Move nine by saving its next reference,
which is None, then point nine back

40
00:02:22,580 --> 00:02:23,540
to seven.

41
00:02:23,780 --> 00:02:28,260
Previous becomes nine and current becomes
None, so the loop stops.

42
00:02:28,460 --> 00:02:30,660
Return previous as the new head.

43
00:02:30,820 --> 00:02:34,553
Following its links now visits nine,
seven, four.

44
00:02:34,607 --> 00:02:37,300
The caller must use this returned
reference.

45
00:02:37,380 --> 00:02:41,780
Its old head still refers to four, which
is now the tail rather than the start of

46
00:02:41,860 --> 00:02:43,620
the complete reversed chain.

47
00:02:43,867 --> 00:02:47,467
Initialize previous to None and current to
the head.

48
00:02:47,507 --> 00:02:50,507
The loop continues while current names a
node.

49
00:02:50,627 --> 00:02:55,587
Save its next reference in following, then
redirect current's next to previous.

50
00:02:55,627 --> 00:02:58,880
Advance previous to current and current to
following.

51
00:02:58,934 --> 00:03:02,067
These assignments must occur in this
dependency order.

52
00:03:02,107 --> 00:03:05,987
The unchanged head argument is not the new
head after reversal.

53
00:03:06,027 --> 00:03:10,427
Return previous after the loop to expose
the complete reversed chain.

54
00:03:10,667 --> 00:03:15,067
The reversal visits every node once and
performs a fixed number of reference

55
00:03:15,347 --> 00:03:16,907
assignments per node.

56
00:03:16,987 --> 00:03:19,840
Its time is linear in the number of nodes.

57
00:03:19,894 --> 00:03:24,427
The three working references use constant
auxiliary space because no second chain is

58
00:03:24,507 --> 00:03:25,574
constructed.

59
00:03:25,600 --> 00:03:30,267
A traversal used to display the values can
allocate a separate list, but that display

60
00:03:30,347 --> 00:03:32,640
is outside the reversal function.

61
00:03:32,694 --> 00:03:35,947
Do not include display storage in the
function unless the submitted

62
00:03:36,347 --> 00:03:38,347
implementation actually creates it.

63
00:03:38,600 --> 00:03:42,360
An empty chain returns None without
entering the loop.

64
00:03:42,440 --> 00:03:47,200
A single node returns that same node with
its next reference still None.

65
00:03:47,240 --> 00:03:51,480
For three nodes, check the new order and
verify that all original node

66
00:03:51,560 --> 00:03:53,587
identities remain present.

67
00:03:53,613 --> 00:03:56,813
The original head must become the final
tail.

68
00:03:56,867 --> 00:03:59,693
This example assumes an acyclic chain.

69
00:03:59,747 --> 00:04:03,720
A cycle would violate the input contract
and requires a different traversal

70
00:04:03,800 --> 00:04:06,200
argument before applying this routine.
