博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode:Reverse Linked List
阅读量:6965 次
发布时间:2019-06-27

本文共 3140 字,大约阅读时间需要 10 分钟。

Reverse a singly linked list.

 

代码如下:

the iterative solution:(c++)

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* reverseList(ListNode* head) {        ListNode *temp = NULL , *nextNode = NULL;        while(head){            nextNode = head->next;   //save the current's next node             head->next = temp;      //let the current point to its previous one            temp = head;           //save the current node as pre            head = nextNode;      //move to next node        }        return temp;            //just point to the last node we wanted    }};

 或:

class Solution {    public:        ListNode *reverseList(ListNode *head){    if (head == NULL || head->next == NULL)        return head;    ListNode *pCurr = head;    ListNode *pPrev = NULL;    ListNode *pNext = NULL;    while (pCurr != NULL)    {        pNext = pCurr->next;  //save next node        pCurr->next = pPrev;        if (pNext == NULL)            head = pCurr;        pPrev = pCurr;        pCurr = pNext;    }    return head;}    };

  

 

其他解法:

 1、 the recursive version:(c++)

class Solution {public:    ListNode* reverseList(ListNode* head) {        if (head == NULL || head->next == NULL) return head;   // case proof and recursion exit conditon        ListNode *np = reverseList(head->next);        head->next->next = head;         // make the next node point back to the node itself        head->next = NULL;               // destroy the former pointer        return np;    }};

 2、(c++)

class Solution {    public:        ListNode* reverseList(ListNode* head) {            stack
s; ListNode *tail=NULL; while(head) { s.push(head); head=head->next; } if(!s.empty()) head=s.top(); while(!s.empty()) { tail=s.top(); s.pop(); if(!s.empty()) tail->next=s.top(); else tail->next=NULL; } return head; } };

  3、(c)

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* reverseList(struct ListNode* head) {        struct ListNode* last = 0;        while (head)        {            struct ListNode* next = head->next;            head->next = last;            last = head;            head = next;        };        return last;    }

 或:

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* reverseList(struct ListNode* head) {    if (!head)        return NULL;    struct ListNode *curr = head;    struct ListNode *next = NULL;    struct ListNode *prev = NULL;    while (curr){        next = curr->next;        curr->next = prev;        prev = curr;        curr = next;        head = prev;    }    return head;}

  

 更多:http://blog.chinaunix.net/uid-7471615-id-83821.html

 

转载于:https://www.cnblogs.com/carsonzhu/p/4589839.html

你可能感兴趣的文章
ARTS打卡计划第6周-REVIEW-超越编码的避免项目失败的软技能
查看>>
【开发技术】java异常的捕获与抛出原则
查看>>
phpwind 去除init.phpwind.net统计功能
查看>>
侧耳倾听
查看>>
数据结构与算法面试题80道(33)
查看>>
jQuery 缺点
查看>>
MFC新建一个窗口
查看>>
SQL中 EXCEPT、INTERSECT用法
查看>>
把boolean 参数放到最后面(Put boolean arguments last)
查看>>
Hemodynamic response function (HRF) - FAQ
查看>>
【小技巧让你的操作系统速度比重装还快】
查看>>
SQL自定义函数split分隔字符串
查看>>
git删除本地所有的更改
查看>>
集合和数据结构
查看>>
js提交数组
查看>>
js发展前史
查看>>
CycleGan
查看>>
cocos2d 安装-mac
查看>>
虚拟机安装问题
查看>>
nodejs的koa2框架
查看>>