首页 > 其他 > 详细

leetcode 206 反转链表 Reverse Linked List

时间:2019-02-27 22:17:34      阅读:169      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

只用了迭代,等会看一下大神的递归解法;

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* reverseList(ListNode* head) {
12         if(head==NULL||head->next==NULL) return head;
13         ListNode* pre,*cur;
14         pre=head;cur=head->next;
15         head->next=NULL;
16         while(cur!=NULL){
17             ListNode* temp;
18             temp=cur->next;
19             cur->next=pre;
20             pre=cur;
21             cur=temp;
22         }
23         return pre;
24     }
25 };

 

leetcode 206 反转链表 Reverse Linked List

原文:https://www.cnblogs.com/joelwang/p/10447288.html

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