zoukankan      html  css  js  c++  java
  • 206 Reverse Linked List 反转链表

    反转一个单链表。
    进阶:
    链表可以迭代或递归地反转。你能否两个都实现一遍?
    详见:https://leetcode.com/problems/reverse-linked-list/description/

    Java实现:

    迭代实现:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            if(head==null){
                return null;
            }
            ListNode cur=head;
            ListNode pre=null;
            ListNode next=null;
            while(cur!=null){
                next=cur.next;
                cur.next=pre;
                pre=cur;
                cur=next;
            }
            return pre;
        }
    }
    

    递归实现:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            if(head==null||head.next==null){
                return head;
            }
            ListNode p=head;
            head=reverseList(p.next);
            p.next.next=p;
            p.next=null;
            return head;
        }
    }
    

     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) {
            if(head==nullptr)
            {
                return nullptr;
            }
            ListNode *cur=head;
            ListNode *pre=nullptr;
            ListNode *next=nullptr;
            while(cur)
            {
                next=cur->next;
                cur->next=pre;
                pre=cur;
                cur=next;
            }
            return pre;
        }
    };
    

     方法二:递归

    /**
     * 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) {
            if(head==nullptr||head->next==nullptr)
            {
                return head;
            }
            ListNode *p=head;
            head=reverseList(p->next);
            p->next->next=p;
            p->next=nullptr;
            return head;
        }
    };
    

     参考:https://www.cnblogs.com/grandyang/p/4478820.html

  • 相关阅读:
    python 装饰器
    git
    JS原生方法实现jQuery的ready()
    js获取css属性方法
    列表页调出点击量
    数组操作
    判断IE版本
    判断IE浏览器用IE条件表达式
    [jQuery] Cannot read property ‘msie’ of undefined错误的解决方法
    复选框字段数组拆分后循环选项值,if判断根据选项值,前端输出html
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8746530.html
Copyright © 2011-2022 走看看