面试中常见链表问题8:重排链表
来源:互联网 发布:淘宝开店身份证要求 编辑:程序博客网 时间:2024/06/15 21:53
给定一个单链表:L0->L1->...->Ln-1->Ln,重新排序以后返回L0->Ln->L1->Ln-1->L2->Ln-2...。 解析:(1)把单链表分割为前后两部分。(2)把后一部分链表反转。(3)把两部分链表交叉插入即可。ListNode * reverseList(ListNode *head){if (head == NULL || head->next == NULL)return head;ListNode *pHead = new ListNode(INT_MAX);pHead->next = head;ListNode *p = head->next;head->next = NULL;while (p){ListNode *tmp = p;p = p->next;tmp->next = pHead->next;pHead->next = tmp;}return pHead->next;}void reorderList(ListNode* &head) {if (head == NULL || head->next == NULL)return;ListNode *slow = head;ListNode *fast = head;while (fast != NULL && fast->next != NULL){fast = fast->next->next;slow = slow->next;}ListNode *pHead1 = head;ListNode *pHead2 = slow->next;slow->next = NULL;pHead2 = reverseList(pHead2);while (pHead2 != NULL){ListNode *tmp2 = pHead1->next;ListNode *tmp = pHead2;pHead2 = pHead2->next;tmp->next = pHead1->next;pHead1->next = tmp;pHead1 = tmp2;}}
0 0
- 面试中常见链表问题8:重排链表
- 面试中常见链表问题_world
- 链表常见面试问题
- 问题 C: 瑞神要考研(重排链表)
- 面试中常见链表问题3:旋转链表
- 面试中常见链表问题6:划分链表
- 面试中常见链表问题13:回文链表
- 面试中常见链表问题12:单链表反转(递归)
- 重排链表
- 重排链表
- 重排链表
- 重排链表
- LintCode:重排链表
- LintCode : 重排链表
- 重排链表
- 重排链表
- 重排链表-LintCode
- 面试中常见的链表题目
- firefox 开启安全禁用端口
- js获取当前域名
- Exception in thread "main" java.lang.IncompatibleClassChangeError: Found class com.squareup.okhttp.C
- 最土团购数据库驱动函数
- 2003服务器,禁止CMD新建用户
- 面试中常见链表问题8:重排链表
- jquery之setTimeout(简单拉幕广告示例)
- IE6出现重复字符(文字溢出)的bug
- jQuery判断是否是隐藏标签
- 为Adobe reader 9.5.1添加书签功能
- ELK之Elasticsearch介绍及在搜索中的使用
- 找到栈最小元素
- Linux shell快速查找PHP木马
- HttpClient 发送 HTTP、HTTPS 请求的简单封装