剑指Offer第十五题:反转链表
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述
输入一个链表,反转链表后,输出新链表的表头。
解答:
思想:三个变量进行反转
public class Q_15 {
public ListNode ReverseList(ListNode head) { if(head==null){ return null; } ListNode sec=head.next; head.next=null; ListNode tmp=null; while(sec!=null){ tmp=sec.next; sec.next=head; head=sec; sec=tmp; } return head; }
}