国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 景德镇市| 泰州市| 昌邑市| 玉树县| 姚安县| 朝阳市| 射洪县| 科尔| 克东县| 宣恩县| 修武县| 广西| 江源县| 巴里| 东兴市| 随州市| 姜堰市| 嘉禾县| 桐庐县| 来凤县| 贡山| 漳平市| 长乐市| 罗城| 莫力| 越西县| 维西| 红原县| 清镇市| 东阳市| 吉安县| 微山县| 二手房| 丁青县| 莆田市| 阜阳市| 石嘴山市| 竹山县| 剑阁县| 竹山县| 马鞍山市|