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
题目描述:
给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。
请尝试使用原地算法完成。你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数。
示例 1:
输入: 1->2->3->4->5->NULL 输出: 1->3->5->2->4->NULL
示例 2:
输入: 2->1->3->5->6->4->7->NULL 输出: 2->3->6->7->1->5->4->NULL
说明:
应当保持奇数节点和偶数节点的相对顺序。 链表的第一个节点视为奇数节点,第二个节点视为偶数节点,以此类推。
解题思路:使用ji,ou两个指针代表奇数链和偶数链的末尾,每次连接完,ji都指向偶数链的首位。
C++解题:
class Solution { public: ListNode* oddEvenList(ListNode* head) { if(!head || !head->next) return head; ListNode* ji = head,*ou = head->next,*ou_head = head->next; while (ji && ou) { ListNode* ji_next = ou->next; if(!ji_next) break; ListNode* ou_next = ou->next ? ou->next->next:NULL; ji->next = ji_next; ou->next = ou_next; ji_next->next = ou_head; ji = ji_next; ou = ou_next; } return head; } };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目描述:
给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。
请尝试使用原地算法完成。你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数。
示例 1:
示例 2:
说明:
解题思路:使用ji,ou两个指针代表奇数链和偶数链的末尾,每次连接完,ji都指向偶数链的首位。
C++解题:
The text was updated successfully, but these errors were encountered: