node* reverseLinkedList (node* head) {
// empty list
if (head == NULL || head -> next == NULL) {
return head;
}
node* prev = NULL;
node* curr = head;
node* forword = NULL;
while (curr != NULL) {
forword = curr -> next;
curr -> next = prev;
prev = curr;
curr = forword;
}
return prev;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter