# Inverting a Singly Linked List Instructor : Prof. Jyh-Shing Roger Jang Designer ： Shao-Huan Wang The ideas are reference to the textbook “Fundamentals.

## Presentation on theme: "Inverting a Singly Linked List Instructor : Prof. Jyh-Shing Roger Jang Designer ： Shao-Huan Wang The ideas are reference to the textbook “Fundamentals."— Presentation transcript:

Inverting a Singly Linked List Instructor : Prof. Jyh-Shing Roger Jang Designer ： Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures in C “.

Inverting a Singly Linked List middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle; x1 x2 x3x4x5 First, initial the middle = NULL ↑ middle NULL ↓ trail NULL

Inverting a Singly Linked List middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle; x1 x2 x3x4x5 First, initial the middle = NULL ↑ middle NULL ↓ trail And continue loop until lead == NULL NULL

Inverting a Singly Linked List middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle; x1 x2 x3x4x5 First, initial the middle = NULL ↑ middle NULL ↓ trail And continue loop until lead == NULL NULL

Inverting a Singly Linked List middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle; x1 x2 x3x4x5 First, initial the middle = NULL ↑ middle NULL ↓ trail And continue loop until lead == NULL NULL

Inverting a Singly Linked List middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle; x1 x2 x3x4x5 First, initial the middle = NULL ↑ middle NULL ↓ trail And continue loop until lead == NULL NULL

Inverting a Singly Linked List middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle; x1 x2 x3x4x5 First, initial the middle = NULL ↑ middle NULL ↓ trail And continue loop until lead == NULL NULL lead == NULL, break the loop