-
剑指Offer (十五):反转链表(Java版)
- 网站名称:剑指Offer (十五):反转链表(Java版)
- 网站分类:技术文章
- 收录时间:2025-08-12 15:52
- 网站地址:
“剑指Offer (十五):反转链表(Java版)” 网站介绍
对于一个单向链表来说,上一条数据只能指向下一条数据(如下图),那我们想反转这个链表,变成下图这个样子,怎么实现呢?
1->2->3->4->5 5->4->3->2->1
首先第一种方式,直接反转顺序,将2->1 ,3->2 以此类推,则完成了链表的反转,具体实现为,定义三个指针,pre,cur,next每次遍历链表的时候,cur指向pre,然后再将cur赋值给pre,next赋值给cur,具体代码如下
private static ListNode firstReverse(ListNode listNode){
if(null == listNode || null == listNode.next){
return listNode;
}
ListNode listNodePre = null;
ListNode listNodeCur = listNode;
ListNode listNodeNext = null;
while (null != listNodeCur){
listNodeNext = listNodeCur.next;
listNodeCur.next = listNodePre;
listNodePre = listNodeCur;
listNodeCur = listNodeNext;
}
return listNodePre;
}
第二种方式,利用栈的特性,先进后出,循环遍历链表进栈,然后依次出栈,组成一个新的链表即可,具体代码如下
private static ListNode secondReverse(ListNode head){
if(null == head || null == head.next){
return head;
}
ListNode listNodeReverse = null;
ListNode listNodeFirst = null;
Stack<ListNode> listNodeStack = new Stack<>();
while (null != head){
listNodeStack.push(head);
head = head.next;
}
if(!listNodeStack.isEmpty()){
listNodeFirst = listNodeStack.pop();
}
listNodeReverse = listNodeFirst;
while (!listNodeStack.isEmpty()){
listNodeReverse.next = listNodeStack.pop();
listNodeReverse = listNodeReverse.next;
}
listNodeReverse.next = null;
return listNodeFirst;
}
第三种,则可以采用递归的方式来解决,如果当前节点为空或者当前节点的下一个节点为空,则返回该节点,不然就将该节点的下一个节点指向当前节点,代码如下
private static ListNode thridReverse(ListNode head){
if(null == head || null == head.next){
return head;
}
ListNode listNodeFirst = head.next;//2
head.next = null;
ListNode listNode = thridReverse(listNodeFirst);
listNodeFirst.next=head;
return listNode;
}
完整代码如下
public class MainReverseListNode {
public static void main(String[] args) {
ListNode listNode0 = new ListNode(1);
ListNode listNode1 = new ListNode(2);
ListNode listNode2 = new ListNode(3);
ListNode listNode3 = new ListNode(4);
listNode0.next = listNode1;
listNode1.next = listNode2;
listNode2.next = listNode3;
//ListNode reverse = firstReverse(listNode0);
ListNode secondReverse = thridReverse(listNode0);
System.out.print(secondReverse.toString());
}
private static ListNode firstReverse(ListNode head){
if(null == head || null == head.next){
return head;
}
ListNode listNodePre = null;
ListNode listNodeCur = head;
ListNode listNodeNext = null;
while (null != listNodeCur){
listNodeNext = listNodeCur.next;
listNodeCur.next = listNodePre;
listNodePre = listNodeCur;
listNodeCur = listNodeNext;
}
return listNodePre;
}
private static ListNode secondReverse(ListNode head){
if(null == head || null == head.next){
return head;
}
ListNode listNodeReverse = null;
ListNode listNodeFirst = null;
Stack<ListNode> listNodeStack = new Stack<>();
while (null != head){
listNodeStack.push(head);
head = head.next;
}
if(!listNodeStack.isEmpty()){
listNodeFirst = listNodeStack.pop();
}
listNodeReverse = listNodeFirst;
while (!listNodeStack.isEmpty()){
listNodeReverse.next = listNodeStack.pop();
listNodeReverse = listNodeReverse.next;
}
listNodeReverse.next = null;
return listNodeFirst;
}
private static ListNode thridReverse(ListNode head){
if(null == head || null == head.next){
return head;
}
ListNode listNodeFirst = head.next;//2
head.next = null;
ListNode listNode = thridReverse(listNodeFirst);
listNodeFirst.next=head;
return listNode;
}
}
更多相关网站
- Entrepreneurship a cornerstone for resilience in Chinese economy
- High-tech boom helps attract foreign investment
- 数据库提速之酒店押金原路退回开发Microsoft DriversPHPSQLServer
- Navigating challenges in Sino-US trade ties
- Forging a path of stability and growth in China-EU relations
- 深入解析Java工厂模式及其应用场景
- PHP高手推荐的几本书(php教程推荐)
- 'China shopping' boom spurred by favorable policies helps drive growth
- 「读书」值得一看的技术类书籍列表
- Humanoid Robots Will Evolve from 'Individual Intelligence' to 'Collective Intelligence', Says UBTECH Founder
- 你见过哪些实用到爆的 Java 代码技巧?
- China has granted visa-free access to citizens of 75 countries: NIA
- 2025 World Robot Conference Kicks Off in Beijing With Record-Breaking Showcase of Humanoid Robots
- 最受程序员推荐的20本书(最受程序员推荐的20本书有哪些)
- Air China narrows first-half loss despite lingering headwinds
- Synchron Unveils World’s First Thought-Controlled iPad Using Apple's Brain Interface Protocol
- IM Motors Bets on Large-Battery Range Extender to Revive Growth Amid Cooling EREV Market
- China's ecological civilization and the road ahead
- 最近发表
-
- Entrepreneurship a cornerstone for resilience in Chinese economy
- High-tech boom helps attract foreign investment
- 数据库提速之酒店押金原路退回开发Microsoft DriversPHPSQLServer
- Navigating challenges in Sino-US trade ties
- Forging a path of stability and growth in China-EU relations
- 深入解析Java工厂模式及其应用场景
- PHP高手推荐的几本书(php教程推荐)
- 'China shopping' boom spurred by favorable policies helps drive growth
- 剑指Offer (十五):反转链表(Java版)
- 「读书」值得一看的技术类书籍列表
- 标签列表
-
- mydisktest_v298 (35)
- sql 日期比较 (33)
- document.appendchild (35)
- 头像打包下载 (35)
- acmecadconverter_8.52绿色版 (25)
- 梦幻诛仙表情包 (36)
- java面试宝典2019pdf (26)
- disk++ (30)
- 加密与解密第四版pdf (29)
- iteye (26)
- centos7.4下载 (32)
- intouch2014r2sp1永久授权 (33)
- jdk1.8.0_191下载 (27)
- axure9注册码 (30)
- virtualdrivemaster (26)
- 数据结构c语言版严蔚敏pdf (25)
- 兔兔工程量计算软件下载 (27)
- 代码整洁之道 pdf (26)
- ccproxy破解版 (31)
- aida64模板 (28)
- engine=innodb (33)
- shiro jwt (28)
- 方格子excel破解版补丁 (25)
- segoe ui是什么字体 (27)
- head first java电子版 (32)