Reverse a singly linked list.
Example:
Input: l-> 2-> 3-> 4-> 5-> NULL Output: 5-> 4-> 3-> 2-> l-> NULL
Problem-solving ideas:
Starting from the head of the list, up three pointers to three adjacent elements, constantly changing direction between the link until a complete traversal of the list
Implementation code:
struct ListNode* reverseList(struct ListNode* head){
struct ListNode* node = head;
struct ListNode* cur = head;
struct ListNode* pre = NULL;
while(cur) {
cur = cur->next;
node->next = pre;
pre = node;
node = cur;
}
return pre;
}
operation result: