码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 206. 反转链表

时间:2019-08-14 12:16:04      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:temp   struct   递归   next   ems   ret   init   targe   ever   

题目链接:https://leetcode-cn.com/problems/reverse-linked-list/

反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
进阶:
你可以迭代或递归地反转链表。你能否用两种方法解决这道题?

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

 

LeetCode 206. 反转链表

标签:temp   struct   递归   next   ems   ret   init   targe   ever   

原文地址:https://www.cnblogs.com/shixinzei/p/11350871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!