328奇偶链表 Posted on 2020-04-19 Words count in article: 187 | Reading time ≈ 1 328.奇偶链表下面是题目下面是题目给出的模板1234567891011121314/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* oddEvenList(ListNode* head) { }}; 看了几种方法,都大同小异,无非是代码量的区别,这里给出一种简单易懂的代码1234567891011121314151617181920212223class Solution {public: ListNode* oddEvenList(ListNode* head) { if(!head) return head; ListNode *even = head->next; ListNode *even_head = head->next; ListNode *odd = head; while(1) { if(even==NULL||even->next==NULL) {//遍历完成后,把odd和even的头节点连起来,再return head odd->next = even_head; return head; } //交错插入,形象的说法就是拉链算法 odd->next = even->next; odd = odd->next; even->next = odd->next; even = even ->next; } }}; -------------本文结束,感谢您的阅读------------- Post author: Jason Post link: https://jasonxqh.github.io/2020/04/19/328%E5%A5%87%E5%81%B6%E9%93%BE%E8%A1%A8/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.