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

链表的奇偶重排

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

全部评论

相关推荐

昨天 11:15
中南大学 Java
好可爱的hr姐姐哈哈哈哈
黑皮白袜臭脚体育生:兄弟们貂蝉在一起,吕布开了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:02
ssob上原来真有BOSS啊
硫蛋蛋:这种也是打工的,只不是是给写字楼房东打工
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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