LeetCode Reverse Linked List (反置链表)

题意:
将单恋表反转。
思路:
两种方法:迭代和递归。
递归
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* give_me_your_son( ListNode* far, ListNode* son)
{
if(!son) return far; //far就是链表尾了
ListNode* tmp=son->next;
son->next=far;
return give_me_your_son( son, tmp);
} ListNode* reverseList(ListNode* head) {
if(!head||!head->next) return head;
return give_me_your_son(,head);
}
};
AC代码
python3
迭代
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
top=None
while head:
top,head.next,head=head,top,head.next
return top
AC代码
递归
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def reverseList(self, head, pre=None):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head: return pre
back=head.next
head.next=pre
return self.reverseList(back, head)
AC代码
LeetCode Reverse Linked List (反置链表)的更多相关文章
- [LeetCode] Reverse Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- [Leetcode] Reverse linked list ii 反转链表
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2 ...
- leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...
- LeetCode Reverse Linked List II 反置链表2
题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交 ...
- LeetCode Reverse Nodes in k-Group 每k个节点为一组,反置链表
题意:给一个单链表,每k个节点就将这k个节点反置,若节点数不是k的倍数,则后面不够k个的这一小段链表不必反置. 思路:递归法.每次递归就将k个节点反置,将k个之后的链表头递归下去解决.利用原来的函数接 ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] 92. Reverse Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- [LeetCode] 92. Reverse Linked List II 反向链表II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- 【原创】Leetcode -- Reverse Linked List II -- 代码随笔(备忘)
题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...
随机推荐
- struts文件上传拦截器maximumSize设置文件大小不起作用
<interceptor-ref name="fileUpload"> <param name="allowedTypes ...
- 剑指offer--面试题15--相关
感受:清晰的思路是最重要的!!! 题目:求链表中间节点 ListNode* MidNodeInList(ListNode* pHead) { if(pHead == NULL) return NULL ...
- oracle——DDL
一.一些概念 定义: 主键--唯一标识一条记录,不能有重复的,不允许为空 外键--表的外键是另一表的主键, 外键可以有重复的, 可以是空值 索引--该字段没有重复值,但可以有一个空值 作用: 主键-- ...
- 【锋利的JQuery-学习笔记】广告栏
效果图: html: <div id="jnImageroll"> <a href="#nogo" id="JS_imgWrap&q ...
- linux源码阅读笔记 jmpi指令(转)
jmpi是段间跳转指令,用于x86实模式下, 如:BOOTSEG = 0x0c70 jmpi 4, #BOOTSEG 假如当前段CS==00h,那么执行此指令后将跳转到段CS==0x0c70,当 ...
- lintcode :最长单词
题目: 最长单词 给一个词典,找出其中所有最长的单词. 样例 在词典 { "dog", "google", "facebook", &quo ...
- [topcoder]BoxesDiv2
https://community.topcoder.com/stat?c=problem_statement&pm=13192 #include <vector> #includ ...
- Sina App Engine(SAE)入门教程(10)- Cron(定时任务)使用
参考资料 SAE Cron说明文档 Cron能干什么? cron 可以定时的触发一个脚本,在sae上最大的频率是一分钟一次.你可以用其来完成自己需要的业务逻辑,例如定期的抓取某些网页完菜信息的采集,定 ...
- css一个图片包含多个图片|网站侧栏导航
<html> <head><title>Hello World</title> <style> .style1{ width:60px;ma ...
- Qt网络通信骨架解析,QtClient QtServer QtSerialPort
http://blog.csdn.net/Dr_Abel/article/details/52469134#t18