java递归实现反转一个单链表

反转一个单链表。

示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

//此题有收获,注意returnValue 的作用(仅用于返回)
public ListNode reverseList(ListNode head) {
		if(head == null)
			return null;
		if(head.next == null)
			return head;	
		
		ListNode next=head.next;
        head.next=null;
		ListNode returnValue = reverseList(next);
		next.next = head;
		
		return returnValue;
	}

猜你喜欢

转载自blog.csdn.net/qq_32534441/article/details/89193249