Reverse Linked List 解答
Question
Reverse a singly linked list.
Solution 1 -- Iterative
Remember to set head.next = null or it will report "memory limit exceeds" error.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
ListNode current = head, prev = head, post = head;
if (current == null || current.next == null)
return current;
current = current.next;
head.next = null;
while (current.next != null) {
post = current.next;
current.next = prev;
prev = current;
current = post;
}
current.next = prev;
return current;
}
}
Solution 2 -- Recursive
We can also use recursion to solve this problem.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode second = head.next;
head.next = null;
ListNode newHead = reverseList(second);
second.next = head;
return newHead;
}
}
Reverse Linked List 解答的更多相关文章
- Reverse Linked List II 解答
Question Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Giv ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [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-206 Reverse Linked List
#206. Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
- 【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-pass. F ...
- [LintCode] Reverse Linked List 倒置链表
Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...
- 14. Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- 【12_206】Reverse Linked List
本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...
随机推荐
- c++ 09
一.数据结构 程序设计=数据结构+算法 1.逻辑结构 1)集合:元素之间没有联系. 2)线性结构:元素之间存在前后顺序. 3)树形结构:元素之间存在一对多的父子关系. 4)图状结构:元素之间存在多对多 ...
- 解决IE6 IE7 JSON.stringify JSON 未定义问题
在项目中引入json2.js 官方http://www.json.org/ 源码地址:https://github.com/douglascrockford/JSON-js $.ajax({ url: ...
- C 本地文件夸网文件Cp操作
1,linux平台C简单实现本地文件cp 码子及运行效果测试
- qt反走样(简选)
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #qt反走样(简选) #概念 """ ...
- 检测iOS系统的定位服务
[CLLocationManager locationServicesEnabled]检测的是整个iOS系统的位置服务开关
- PC-CSS-默认字体样式
字体样式:Arial:字体大小:12px;行高:1.5倍:
- Bin & Jing in wonderland(概率,组合数学)
Problem 2103 Bin & Jing in wonderland Accept: 201 Submit: 1048 Time Limit: 1000 mSec Memor ...
- mac下显示隐藏文件
一.在终端中 ls -a就可以查看隐藏文件. 显示和隐藏的命令例如以下: 显示:defaults write com.apple.finder AppleShowAllFiles -bool true ...
- char值码对应大全
Char("0") 为0的字符Char("1") Char("2") Char("3") Char("4&qu ...
- struts1面试题
由于找了很久的工作都没有找的,只能四处收集那个面试题的.和看面试题的 还有那个记忆力也不是很好了的,而那些公司面试的时候总会有一个面试题的! 在这里分享给大家(那个本来是想上传文件的,但是找不到的 ...