反转链表

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

代码如下:

 1 package com.feimao.com.feimao.a2.test;
 2 
 3 import com.feimao.linkedlist.test.Node;
 4 
 5 public class ReverseList {
 6     public static Node reverseList(Node node){
 7         //判断只有1个节点和空链表的情况
 8         if((head == null)||(head.next == null))
 9             return head;
10         Node pre = null;
11         Node next = null;
12         while (head != null){
13             next = head.next;
14             head.next = pre;
15             pre = head;
16             head = next;
17         }
18         return pre;
19     }
20 }

以这道题为例,至少要想到以下几类测试case

1.输入链表头结点的指针是null

2.输入链表只有1个结点

3.输入链表有多个结点

原文地址:https://www.cnblogs.com/feimaoyuzhubaobao/p/10200028.html