这道题弄的心好累。。

 【Reverse Linked List】206

描述:

Reverse a singly linked list.

click to show more hints.

Hint:

A linked list can be reversed either iteratively or recursively. Could you implement both?

思路:

增加一个全局节点newHead,用来指向新的头部,用用一个临时节点next来遍历整个链表,用来当旧的头部head,例如链表1-2-3-4,第一遍循环,newhead指向1,指向null,head是2,指向剩余的链表,2-3-4,然后第二遍循环,newhead指向2-1,head指向剩余的链表,3-4。。。以此类推,直到循环结束。

// iterative solution

public ListNode reverseList(ListNode head) {
ListNode newHead = null;
while(head != null){
ListNode next = head.next;
head.next = newHead;
newHead = head;
head = next;
}
return newHead;
}

还有递归的算法:

// recursive solution
public ListNode reverseList(ListNode head) {
return reverseListInt(head, null);
} public ListNode reverseListInt(ListNode head, ListNode newHead) {
if(head == null)
return newHead;
ListNode next = head.next;
head.next = newHead;
return reverseListInt(next, head);
}

leetcode day7的更多相关文章

  1. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  2. leetcode每日刷题计划-简单篇day7

    还没有背单词,头晕脑胀 Num 66 加一 Plus One 注意就是进位的时候最后一位,为了省两句代码,那几个语句顺序写反覆盖的乱七八糟 vector头部插入(a.begin(),被插入的数) 如果 ...

  3. 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和

    [算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...

  4. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  6. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  7. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  8. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

随机推荐

  1. swift(2)元祖(Tuple)

    let somePoint = (, ) switch somePoint { , ): // 位于远点 println("(0, 0) is at the origin") ): ...

  2. VirtualBox中CentOS通过Host-Only方式实现虚拟机主机互相访问、共享上网

    VirtualBox常用的网络配置如下: 连接方式 主机访问虚拟机 虚拟机访问主机 虚拟机访问虚拟机 虚拟机访问外网 说明 网络地址转换(NAT) 不支持 支持 不支持 支持 默认连接方式,虚拟IP, ...

  3. ORACLE中CHAR、VARCHAR、NVARCHAR

    1. char      固定长度,最长n个字符.   2. varchar      最大长度为n的可变字符串. (n为某一整数,不同数据库,最大长度n不同)   char和varchar区别:   ...

  4. HDU 5889 (最短路+网络流)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  5. Centos6.5安装与配置Tomcat-8的方法

    环境要求: 系统: [root@Wulaoer ~]# cat /proc/version Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bs ...

  6. js所有函数集合

    lick() 对象.click() 使对象被点击. closed 对象.closed 对象窗口是否已关闭true/false clearTimeout(对象) 清除已设置的setTimeout对象 c ...

  7. Android中监听webview监听是否加载完成

    之前写过一篇捕获Phoengap的webview事件的方法,主要是在实现了CordovaInterface的Activity中,  在onMessage中根据第一个参数的message name来判断 ...

  8. 低电压锁定(UVLO) (转)

    源:http://blog.csdn.net/zhenwenxian/article/details/8523307 UVLO就是低电压锁定: 低压关断. 欠压关断模式是当供电电压低于IC的开启门限电 ...

  9. 饥饿的牛(hunger)

    饥饿的牛(hunger) 题目描述 牛在饲料槽前排好了队.饲料槽依次用1到n(1≤n≤2000)编号.每天晚上,一头幸运的牛根据约翰的规则,吃其中一些槽里的饲料.约翰提供B个区间的清单.一个区间是一对 ...

  10. 线程pthread_create()、pthread_exit()、pthread_join()、pthread_cancel()