国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

328. Odd Even Linked List

2019-11-08 03:00:36
字體:
來源:轉載
供稿:網友

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

You should try to do it in place. The PRogram should run in O(1) space complexity and O(nodes) time complexity.

Example: Given 1->2->3->4->5->NULL, return 1->3->5->2->4->NULL.

Note: The relative order inside both the even and odd groups should remain as it was in the input. The first node is considered odd, the second node even and so on …

s思路: 1. 這個簡單啊。由于是分成兩個鏈表后再連接起來,那么先找到兩個鏈表的頭,然后按照交叉的順序一步一步的分解成兩條鏈表,由于是很規(guī)則的操作,所以也沒啥幺蛾子。

class Solution {public: ListNode* oddEvenList(ListNode* head) { // if(!head) return NULL; ListNode* head1=head,*head2=head->next;//bug:看到head->next,就要自然反映這是在裸奔,沒有加保護的裸奔,所以要把保護加在前面! ListNode* odd=head1,*even=head2; while(even&&even->next){//注意: odd->next=even->next; odd=odd->next; if(odd){ even->next=odd->next; even=even->next; } } odd->next=head2; return head; }};
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 佛学| 神池县| 余干县| 正阳县| 偃师市| 古浪县| 西青区| 太原市| 县级市| 兴仁县| 南陵县| 会宁县| 靖州| 阳原县| 宁化县| 二连浩特市| 绥德县| 桑日县| 信宜市| 八宿县| 边坝县| 建湖县| 巴马| 盘锦市| 汨罗市| 绍兴市| 全椒县| 陆良县| 周至县| 综艺| 临武县| 岳池县| 凤山市| 厦门市| 扬州市| 新河县| 邵武市| 二手房| 大丰市| 将乐县| 明星|