题解 | #链表的奇偶重排#

链表的奇偶重排

https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3

//双队列,if(i%2)----->容易理解错,奇数成立,偶数不成立
class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
		ListNode* nList=new ListNode(0);
		ListNode* nHead = nList;
		ListNode* tail=head;
		int len = 0;
		queue<ListNode*>sig;
		queue<ListNode*>dou;
		while (tail)
		{
			tail = tail->next;
			len++;
		}
		tail = head;
		for ( int i=1;i<=len; i++)
		{
			if (i % 2)
			{
				dou.push(tail);
				tail = tail->next;
			}
			else
			{
				sig.push(tail);
				tail = tail->next;
			}
		}
		tail = head;
		while (!dou.empty())
		{
			nList->next = dou.front();
			dou.pop();
			nList = nList->next;
		}
		while (!sig.empty())
		{
			nList->next = sig.front();
			sig.pop();
			nList = nList->next;
		}

		nList->next = nullptr;
		return nHead->next;
    }
};

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 11:21
被夸真的超级开心,好可爱的姐姐
码农索隆:老色批们不用脑补了,我把金智妮的图找来了查看图片
点赞 评论 收藏
分享
昨天 14:00
门头沟学院 运营
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务