面试16: 反转链表
来源:互联网 发布:武术器材专卖店淘宝网 编辑:程序博客网 时间:2024/05/24 23:12
一. 题目
定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点.
代码请到我的代码库中下载 Point2Offer
二. 代码
package com;/** * 剑指offer: 链表反转 * 两种方法求解:1.遍历;2.递归. * 测试用例三种(链表有多个节点,一个节点和链表为空) * @author dingding * Date:2017-6-14 9:55 * Declaration: All Rights Reserved! */public class ReverseList { public static void main(String[] args) { test1(); test2(); test3(); } private static class ListNode{ int value; ListNode next; } //遍历的方式进行反转,返回反转后的头结点(pReversedHead) private static ListNode reverseListIteratively(ListNode pHead){ ListNode pReversedHead = null; ListNode pNode = pHead; ListNode pPrev = null; while(pNode!=null){ ListNode pNext = pNode.next; if (pNext==null) { pReversedHead = pNode; } pNode.next = pPrev; pPrev = pNode; pNode = pNext; } return pReversedHead; } //递归方法进行反转,返回下一个递归的节点 private static ListNode reverseListRecursively(ListNode pHead){ if (pHead==null) { //System.out.println("链表为空!"); return null; }else { while (pHead!=null && pHead.next==null){ return pHead; } ListNode pNode = reverseListRecursively(pHead.next); pHead.next.next=pHead; pHead.next=null; return pNode; } } private static void printList(ListNode pHead){ if (pHead==null) { System.out.println("链表为空!"); }else { while(pHead!=null){ System.out.print(pHead.value+" "); pHead = pHead.next; } } System.out.println(); } /*================测试代码================================ */ private static void test(ListNode first){ System.out.println("初始列表:"); printList(first); System.out.println("遍历方法反转结果:"); ListNode pTail = reverseListIteratively(first); printList(pTail); System.out.println("递归方法反转结果:"); ListNode pNode = reverseListRecursively(pTail); printList(pNode); } //输入的链表有多个节点 private static void test1(){ ListNode first = new ListNode(); ListNode second = new ListNode(); ListNode third = new ListNode(); ListNode fourth = new ListNode(); ListNode fifth = new ListNode(); first.value = 1; second.value = 2; third.value = 3; fourth.value = 4; fifth.value = 5; first.next = second; second.next = third; third.next = fourth; fourth.next = fifth; test(first); } //输入的链表只有一个结点 private static void test2(){ ListNode first = new ListNode(); first.value=1; test(first); } //输入空链表 private static void test3(){ test(null); }}
链表类
package com;/** * 链表类 * @author dingding * */public class ListNode { public int value; public ListNode next; public ListNode(int value) { this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public ListNode getNext() { return next; } public void setNext(ListNode next) { this.next = next; }}
有不妥当之处,麻烦告知:D
阅读全文
0 0
- 面试16: 反转链表
- 剑指offer面试16 反转链表
- 剑指offer-3-面试16:反转链表
- [面试] [反转链表] 三变量提头法
- 链表反转 58面试“留念”
- 面试算法(十五)反转链表
- 链表相关面试题目 反转 合并
- 面试100题---1反转链表
- 【WG面试算法】链表反转
- 面试常见算法之---反转链表
- [程序员面试金典]-链表反转
- 16:反转链表
- 16 反转链表
- 16-反转链表
- 【我要去面试】单向链表反转
- 程序员面试宝典(19)-反转链表
- 面试中常见链表问题12:单链表反转(递归)
- 面试 考虑链表反转的递归实现
- AntShares 区块链的智能合约(FunctionCode)外传数据
- 程序设计实践课的感受
- C++ STL容器参考手册 (总册)
- Java中的try-catch-finally
- markdown语法
- 面试16: 反转链表
- 学习是程序员的必修课
- caffe mnist loss函数的理解
- 获取应用程序的签名,防止二次打包
- C语言实现一个自动刷弹幕的程序
- Linux下搭建集群环境(1)-----------linux下安装JDK1.7
- NAT技术与代理服务器
- 软件设计之UML—UML中的六大关系
- 锋利的jQuery读书笔记-第7章 jQuery插件的使用和写法