LeetCode:Reorder List C++与Java实现
来源:互联网 发布:淘宝小号赚钱 编辑:程序博客网 时间:2024/06/05 02:11
LeetCode:Reorder List
题目(题目链接):
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes' values.
For example,
Given {1,2,3,4}
, reorder it to {1,4,2,3}
.
分析:先用快慢指针找到链表的中点,然后翻转链表后半部分,再和前半部分组合。需要注意的是把链表分成两半时,前半段的尾节点要置为NULL,翻转链表时也要把尾节点置为NULL。代码如下:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: void reorderList(ListNode *head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(head == NULL || head->next == NULL)return; ListNode *fastp = head, *lowp = head, *tail = NULL; while(fastp != NULL && fastp->next != NULL) {//利用快慢指针找到链表的中点 tail = lowp; fastp = fastp->next->next; lowp = lowp->next; } tail->next = NULL; //此时tail 指向前半段的结尾 reverseList(lowp);//翻转链表后半段 fastp = head; tail = NULL; while(fastp != NULL) { ListNode *tmp = lowp->next; lowp->next = fastp->next; fastp->next = lowp; tail = lowp; fastp = lowp->next; lowp = tmp; } if(lowp != NULL) tail->next = lowp; } void reverseList(ListNode* &head) {//翻转链表 if(head == NULL || head->next == NULL)return; ListNode *pre = head, *p = pre->next; while(p != NULL) { ListNode *tmp = p->next; p->next = pre; pre = p; p = tmp; } head->next = NULL; head = pre; }};
网上一般都是C++实现,自己做了个java版本,算法不同,网上也有同样算法的C++版。
代码如下:
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */import java.util.*;public class Solution { public void reorderList(ListNode head) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(head==null||head.next==null||head.next.next==null) return; Vector<ListNode> vin=new Vector<ListNode>(); Vector<ListNode> vout=new Vector<ListNode>(); ListNode p=head; while(p!=null) { vin.add(p); p=p.next; } int i=0; int n=vin.size(); int k=n-1; for(;k>n/2;i++,k--) { vout.add(vin.get(i)); vout.add(vin.get(k)); } while(i<n/2+1) { vout.add(vin.get(i)); i++; } int j=0; for(;j<vout.size()-1;j++) { vout.get(j).next=vout.get(j+1); } vout.get(j).next=null; }}
- LeetCode:Reorder List C++与Java实现
- leetcode reorder-list(java实现)
- [Leetcode] Reorder List (Java)
- Reorder List leetcode java
- ReOrder List java实现
- [C++]LeetCode: 75 Reorder List
- [leetcode-143]Reorder List(c)
- LeetCode Reorder List Java版本
- [LeetCode] 143. Reorder List java
- 【LeetCode】143. Reorder List C语言
- leetcode: Reorder List 的Python实现
- [leetcode][list] Reorder List
- [leetcode][list] Reorder List
- LeetCode OJ --问题与解答 Reorder List
- <LeetCode(Java版)>Reorder List
- [LeetCode]--143. Reorder List(Python + Java)
- leetcode oj java 143. Reorder List
- C实现 LeetCode->Reorder List (双指针大法)(单链表是否有环)
- 第六节cocos2dx的小试牛刀
- 关于 plsql 的总结 (一)
- openjdk和jdk的区别与联系
- 其他TM:biterm topic model
- MyEclipse设置编码方式
- LeetCode:Reorder List C++与Java实现
- Linux下配置NTP时间服务器
- 第七节cocos2dx的架构揣测
- python tab键自动补全
- C++构造函数
- ring3 char 字符数组 传入ring0后 转化为UNICODE_STRING处理
- 深入理解软件构建系统原理与最佳实践阅读笔记
- MyEclipse创建HTML5移动应用程序全过程
- if分支语句优化