LintCode 翻转链表
2017-09-22 16:42
363 查看
翻转一个链表
样例
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null
样例
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /* * @param head: n * @return: The new head of reversed linked list. */ public ListNode reverse(ListNode head) { if (head==null){ return head; } ListNode p=head; ListNode next=head.next; ListNode pre=new ListNode(0); pre.next=head; while(p!=null){ next=p.next; p.next=pre; pre=p; p=next; } head.next=null; return pre; } }
相关文章推荐
- 35. 翻转链表(reverse-linked-list)(c++)----lintcode面试题之链表
- lintcode 中等题: reverse linked list II 翻转链表II
- LintCode容易题练习——35. 翻转链表
- 翻转链表 II-LintCode
- LintCode容易题练习——35. 翻转链表
- K组翻转链表-LintCode
- LintCode 翻转链表
- Lintcode:35. 翻转链表
- Lintcode 35.翻转链表
- 翻转链表-LintCode
- LintCode_035_翻转链表
- Lintcode 翻转链表
- LintCode-翻转链表
- LintCode刷题阶段性总结---链表容易篇(翻转链表,删除链表重复元素,合并两个排序链表等)
- LintCode_035_翻转链表
- Lintcode 35.翻转链表
- [Lintcode]翻转链表
- lintcode 35. 翻转链表
- lintcode 35. 翻转链表
- [LintCode]450.K组翻转链表