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

链表的奇偶重排

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;
    }
};

全部评论

相关推荐

02-10 12:23
已编辑
新余学院 C++
采集想要offer:专业技能那里要一条一条的列出来吧,感觉你项目很厉害了,但是如果你不写技术栈面试官对你项目不太懂的话都没办法问你八股😂C++都是基架岗,都是一群9✌🏻在卷,我觉得你要是有时间学个go把MySQL和redis写上去找个开发岗吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务