面试16: 反转链表

一. 题目

定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点.

代码请到我的代码库中下载 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值