lintcode-easy-Reverse Linked List
2016-03-06 10:37
375 查看
Reverse a linked list.
For linked list
For linked list
1->2->3, the reversed linked list is
3->2->1
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param head: The head of linked list. * @return: The new head of reversed linked list. */ public ListNode reverse(ListNode head) { // write your code here if(head == null || head.next == null) return head; ListNode prev = null; ListNode curr = head; ListNode next = head.next; while(next != null){ curr.next = prev; prev = curr; curr = next; next = next.next; } curr.next = prev; return curr; } }
相关文章推荐
- MongoDB监控项--如何使用命令得到
- jmp_buf的使用,结构定义为数组
- HDU 5637 Transform 搜索
- 工厂模式理解
- 自学 java 笔记 day9(继承下)
- leetcode:Best Time to Buy and Sell Stock II 【Java】
- CentOS 7 配置163源
- linux安全第二周总结
- Python切片知识解析
- [Unity优化]数据的加密与解密
- SQL读书笔记(九) UNION
- hihoCoder#1032 : 最长回文子串(manacher算法)
- kidd风的IOS日志之地图与定位
- Android中百度地图的使用方法之覆盖物功能(一)
- Kiss you goodbye
- 简单的验证码实现
- 分治法——归并排序
- 从无重复大数组找TOP N元素的最优解说起
- html5中链接和分组标签
- Krustral 算法