We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Description
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list's nodes, only nodes itself may be changed.
Example:
Given 1->2->3->4, you should return the list as 2->1->4->3.
解题思路:这题比较简单,画图做好每个指针的指向就好了,别丢失了指针,这题还可用递归处理,参考 grandyang/leetcode#24
C解题:
struct ListNode* swapPairs(struct ListNode* head){ struct ListNode* current = head; struct ListNode* current_prefix; struct ListNode* result = NULL; while (current) { if (!head->next) break; if(!result){//头结点的处理 result = head->next; head->next = head->next->next; result->next = head; }else{ if(current->next){ current_prefix->next = current->next; current->next = current->next->next; current_prefix->next->next = current; } } current_prefix = current; current = current -> next; } if(!result) return head; return result; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Description
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list's nodes, only nodes itself may be changed.
Example:
解题思路:这题比较简单,画图做好每个指针的指向就好了,别丢失了指针,这题还可用递归处理,参考 grandyang/leetcode#24
C解题:
The text was updated successfully, but these errors were encountered: