【单链表OJ题:反转链表】
题目来源
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/
struct ListNode* reverseList(struct ListNode* head){struct ListNode* current = head;struct ListNode* newnode = NULL;while(current!=NULL){struct ListNode* next = current->next;current->next = newnode;newnode = current;current = next;}return newnode;
}
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* reverseList(struct ListNode* head){//反转链表使用头插法//需要使用三个指针struct ListNode* begin = head;struct ListNode* end = NULL;if(begin!=NULL)end = begin->next;struct ListNode* newhead = NULL;while(begin!=NULL){begin->next = newhead;newhead = begin;begin = end;if(end!=NULL)end = end->next;}return newhead;
}