23.反转链表
给定单链表的头节点 head ,请反转链表,并返回反转后的链表的头节点。
示例 1:

输入:head = [1,2,3,4,5] 输出:[5,4,3,2,1]

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode reverseList(ListNode head) { if(head==null) return null; ListNode tt = null; ListNode temp = head; ListNode headA = null; while(temp != null){ tt=temp.next; temp.next = headA; headA=temp; temp=tt; } return headA; } }

浙公网安备 33010602011771号