Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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:

1 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 “.

2 Inverting a Singly Linked List middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle; x1 x2 x3x4x5 NULL

3 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

4 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

5 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

6 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

7 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

8 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

9 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 NULL And continue loop until lead == NULL NULL ↑ middle ↓ trail return middle as the lead of linked list lead == NULL, break the loop


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

Similar presentations


Ads by Google