Reverse Linked List以及一道有关倒序的题目
来源:互联网 发布:眼镜搭配脸型软件 编辑:程序博客网 时间:2024/05/16 09:43
一.Reverse a singly linked list.
Hint:
二.
然后我发现了我们可以把后半部分倒序,然后插入到前半部分,就可以完成这道题目
代码如下:
A linked list can be reversed either iteratively or recursively. Could you implement both?
递归法:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* reverseList(ListNode* head) { if(head&&head->next){ ListNode*p=head->next; head->next=nullptr; ListNode*newhead=reverseList(p); p->next=head; return newhead; } return head; }};
每次把第一个节点扔到最后,循环到最终状态
迭代法:
(1) Create a Node named prehead
before head
and locate the immediate node before them
-th (notice that it is1
-indexed) node pre
;
(2) Set cur
to be the immediate node after pre
and at each time move the immediate node aftercur
(namedmove
) to be the immediate node after pre
. Repeat it forn - m
times.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* reverseList(ListNode* head) { ListNode prehead(0),*pre=&prehead,*cur,*move; pre->next=head; cur=pre->next; while(cur&&cur->next) { move=cur->next; cur->next=move->next; move->next=pre->next; pre->next=move; } return prehead.next; }};
二.
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}
.
class Solution {public: void reorderList(ListNode* head) { if(head!=nullptr&&head->next!=nullptr){ ListNode *p=head,*tail=head->next; while(tail->next) { p=p->next; tail=tail->next; } p->next=nullptr; reorderList(head->next); tail->next=head->next; head->next=tail; } }};
然后我发现了我们可以把后半部分倒序,然后插入到前半部分,就可以完成这道题目
代码如下:
/** * 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) { if(head!=nullptr&&head->next!=nullptr&&head->next->next!=nullptr){ ListNode *pre,*tail,*cur,*move; pre=head; tail=head; int sum=0; while(pre) { pre=pre->next; sum++; } pre=head; sum=sum/2; for(int i=0;i<sum-1;i++) pre=pre->next; cur=pre->next; while(cur->next) { move=cur->next; cur->next=move->next; move->next=pre->next; pre->next=move; } pre->next=nullptr; pre=head; while(cur!=nullptr) { cur=pre->next; pre->next=move; pre=move; move=cur; } }}};
阅读全文
0 0
- Reverse Linked List以及一道有关倒序的题目
- 【一天一道LeetCode】#206. Reverse Linked List
- 22.leetcode题目206: Reverse Linked List
- Easy-题目12:206. Reverse Linked List
- 一道有关static的题目
- Reverse Linked List II --一道很锻炼链表和指针的题
- 【一天一道LeetCode】#92. Reverse Linked List II
- Middle-题目77:92. Reverse Linked List II
- 206. Reverse Linked List的C++解法
- 一道有关图论的题目
- 一道与预处理有关的题目
- 一道与死锁有关的题目
- Single linked list reverse
- reverse a linked list
- Reverse a linked list.
- Reverse a linked list
- Reverse Linked List II
- Reverse Linked List II
- Extjs actioncolumn动态显示按钮
- 阿里云CentOS7 搭建 VPN
- Memcached统计命令
- GreenDao3.0的使用
- java学习笔记--多线程
- Reverse Linked List以及一道有关倒序的题目
- Win下Eclipse提交hadoop程序出错:org.apache.hadoop.security.AccessControlException: Permission denied: user=
- lib/arm64, /vendor/lib64 couldn't find " .so"
- 获取当前日期上一周开始时间、结束时间和年份
- redis安装与部署
- React介绍
- PHP操作Memcached
- 泡妞秘籍一
- Linux 磁盘管理(2)