#include<iostream>
using namespace std;struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};class Solution {
public://翻转链表ListNode* reverseList(ListNode* head) {ListNode* cur = head;ListNode* new_head = NULL;while (cur){ListNode* temp = cur;cur = cur->next;temp->next = new_head;new_head = temp;}head = new_head;return head;}void reorderList(ListNode* head) {if (head == NULL || head->next == NULL)return;ListNode* slow = head;ListNode* fast = head;ListNode* last = head;//得到list的中点while (fast&&fast->next){last = slow;slow = slow->next;fast = fast->next->next;}last->next = NULL;last = NULL;//后半段反转slow=reverseList(slow);fast = head;ListNode* temp = NULL;while (fast){temp = slow;slow = slow->next;temp->next = fast->next;fast->next = temp;last = fast->next;fast = fast->next->next;}if (slow)last->next = slow;}ListNode* createList(ListNode* head){int numOfNode;int value;cout << "please input number of listNode:";cin >> numOfNode;cin >> value;head = new ListNode(value);ListNode* cur = head;for (int i = 1; i < numOfNode; ++i){cin >> value;ListNode* temp = new ListNode(value);cur->next = temp;cur = temp;}return head;}void printNode(ListNode* head){ListNode* cur = head;while (cur){cout << cur->val << " ";cur = cur->next;}cout << endl;}
};int main(){ListNode* head = NULL;Solution lst;head = lst.createList(head);lst.printNode(head);lst.reorderList(head);lst.printNode(head);system("pause");return 0;
}
详细解决方案
【leetcode】【单链表】【143】Reorder List
热度:13 发布时间:2023-12-21 18:06:27.0
相关解决方案
- request.setAttribute("list" "''");该怎么解决
- Hibernate 的query.list()的有关问题
- List 的removeAll方法如何用
- 关于在struts2中怎么将.action加入到web.xml中的<welcome-file-list>上 急
- List items = upload.parseRequest(request);替空kindeditor SpringMvc
- 在JSP中,List<String> str_list这个集合如何遍历呢
- 关于<welcome-file-list>的疑问。初学者郁闷哦
- hibernate Query.createQuery(queryString).list();查询最大只有1000条数据,该怎么处理
- list 接收数据库数据如何写
- hibernate query.list() 不执行解决方法
- list 对象空指针错误
- jsp页面能使用List list= new List()吗,该怎么解决
- java口试要点-ArrayList list = new ArrayList(); 在这个泛型为Integer的ArrayList中存放一个String类型的对象
- 设计可组建的j2me UI(五) List
- j2me list 设置内容格式?解决方案
- list 在struts 2.1 jsp中如何显示?
- JAVA List<Map<String,String>> 排序解决思路
- 关于java List Map方面的简单有关问题
- List<String>取值为什么带“[]”了?解决思路
- java List<>排序有关问题
- Unknown table 'book' in field list
- at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
- List<Object> list=new ArrayList<String>()异常
- java.util.List 求详解,该如何解决
- List 复制速度,该如何解决
- List<User> userList = new ArrayList<User>();解决思路
- List list = new ArrayList(); 是什么意思?解决思路
- 泛型里边的<T> List<T>前面的<T>代表是什么意思?为什么要加<T>
- list,set,地图的作用
- List Control 或者Listbox 中重绘的有关问题