翻转链表
来源:互联网 发布:banner设计软件 编辑:程序博客网 时间:2024/05/14 14:08
问题描述:
翻转一个链表
样例
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null
解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: n * @return: The new head of reversed linked list. */ public ListNode reverse(ListNode head) { // write your code here if(head==null) return null; ListNode node=new ListNode(0); while(head!=null) { ListNode tmp=head.next; ListNode next=node.next; node.next=head; node.next.next=next; head=tmp; } return node.next; }}
解2:
/** * 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) { // write your code here if(head==null) return null; ListNode node=new ListNode(head.val); head=head.next; while(head!=null) { ListNode tmp=head.next; head.next=node; node=head; head=tmp; } return node; }}
阅读全文
0 0
- 无情链表的创建,,翻转翻转
- 链表翻转程序
- 链表翻转
- 链表翻转
- 链表翻转
- 翻转单向链表
- 翻转链表
- 原地翻转链表
- 链表的翻转
- 链表翻转
- 翻转部分链表
- 链表翻转
- 链表翻转
- c++翻转链表
- 翻转链表
- 链表翻转
- 翻转链表
- 链表翻转
- MySQL 基本操作--DML:数据操作语句(insert、delete、update)
- PAT 1105. Spiral Matrix (25) 螺旋矩阵,二维数组的一维应用及边界模拟
- 指针数组与数组指针
- js获取url上的参数
- 题目7-街区最短路径问题
- 翻转链表
- 宽带带宽和下载速度
- Android百度地图(六):百度地图POI检索,行政区边界、公交、线路规划查询,地理编码介绍
- 关键字,操作符和函数区别
- HttpMessageConverters-spring-boot(1.5.6)-cloudfeign1.3.4 学习笔记
- ubuntu下启用root登录
- Java语法基础练习题2
- 2017年9月16日提高组T2 A
- Python安全小工具之生成高精度字典