90 likes | 237 Views
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. NULL. middle = NULL;. while (lead) {. trail = middle;. middle = lead;.
E N D
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 NULL middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle;
Inverting a Singly Linked List ↓trail NULL NULL ↑middle First, initial the middle = NULL middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle;
Inverting a Singly Linked List ↓trail NULL NULL ↑middle First, initial the middle = NULL middle = NULL; while (lead) { And continue loop until lead == NULL trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle;
Inverting a Singly Linked List ↓trail NULL NULL ↑middle First, initial the middle = NULL middle = NULL; while (lead) { And continue loop until lead == NULL trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle;
Inverting a Singly Linked List ↓trail NULL NULL ↑middle First, initial the middle = NULL middle = NULL; while (lead) { And continue loop until lead == NULL trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle;
Inverting a Singly Linked List ↓trail NULL NULL ↑middle First, initial the middle = NULL middle = NULL; while (lead) { And continue loop until lead == NULL trail = middle; middle = lead; lead = lead->link; middle->link = trail; } return middle;
Inverting a Singly Linked List ↓trail NULL NULL ↑middle First, initial the middle = NULL middle = NULL; while (lead) { And continue loop until lead == NULL trail = middle; lead == NULL, break the loop middle = lead; lead = lead->link; middle->link = trail; } return middle;
Inverting a Singly Linked List ↓trail NULL NULL ↑middle First, initial the middle = NULL middle = NULL; while (lead) { And continue loop until lead == NULL trail = middle; lead == NULL, break the loop middle = lead; return middle as the lead of linked list lead = lead->link; middle->link = trail; } return middle;