zoukankan      html  css  js  c++  java
  • leetcode:Reverse Linked List

    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<ListNode*> 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

  • 相关阅读:
    Codeforces Gym 100571A A. Cursed Query 离线
    codeforces Gym 100500 J. Bye Bye Russia
    codeforces Gym 100500H H. ICPC Quest 水题
    codeforces Gym 100500H A. Potion of Immortality 简单DP
    Codeforces Gym 100500F Problem F. Door Lock 二分
    codeforces Gym 100500C D.Hall of Fame 排序
    spring data jpa 创建方法名进行简单查询
    Spring集成JPA提示Not an managed type
    hibernate配置文件中的catalog属性
    SonarLint插件的安装与使用
  • 原文地址:https://www.cnblogs.com/carsonzhu/p/4589839.html
Copyright © 2011-2022 走看看