首页 > 其他 > 详细

LeetCode 面试题24. 反转链表

时间:2020-02-27 17:51:50      阅读:95      评论:0      收藏:0      [点我收藏+]

题目链接:https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/

定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。

示例:

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

限制:

0 <= 节点个数 <= 5000

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 
 9 struct ListNode* reverseList(struct ListNode* head){
10     if(head==NULL||head->next==NULL) return head;
11     struct ListNode *cur=head->next,*pre=head,*tmp;
12     pre->next=NULL;
13     while(cur){
14         tmp=cur->next;
15         cur->next=pre;
16         pre=cur;
17         cur=tmp;
18     }
19     return pre;
20 }

 

LeetCode 面试题24. 反转链表

原文:https://www.cnblogs.com/shixinzei/p/12373299.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!