剑指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;
}

}

全部评论

相关推荐

收拾收拾准备躺平咯
在秋招的河老师很爱吃:兄弟你吃拼好饭中毒了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务