翻转单链表

it2023-03-07  79

翻转单链表

题目描述: 给定一个单链表1-2-3-4-5,翻转成5-4-3-2-1。

public ListNode reverse(ListNode head){ if(head==null) return head; ListNode tail=head; head=head.next; tail.next=null; while(head!=null){ ListNode temp=head.next; head.next=tail; tail=head; head=temp; } return tail; }
最新回复(0)