栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

LeetCode刷题--点滴记录009

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LeetCode刷题--点滴记录009

9. 剑指 Offer 24. 反转链表

要求

定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。

解题

C++版本
#include 
using namespace std;

struct ListNode {
    int val;        // 节点值
    ListNode* next; // 后继节点引用
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(NULL) {}
    ListNode(int x, ListNode*next) : val(x), next(next) {}
};

void printList(ListNode* phead)//输出
{
    ListNode* p = phead;
    while (p != NULL)
    {
        cout << p->val << " ";
        p = p->next;
    }
    cout << endl;
}

class Solution {
public:
    ListNode* reverseList01(ListNode* head) {
        ListNode* cur = head;
        ListNode* pre = nullptr;
        while (cur != nullptr) {
            ListNode* tmp = cur->next; // 暂存后继节点 cur.next
            cur->next = pre;           // 修改 next 引用指向
            pre = cur;                 // pre 暂存 cur
            cur = tmp;                 // cur 访问下一节点
        }
        return pre;
    }

    ListNode* reverseList02(ListNode* head)
    {
        if (head == nullptr||head->next == nullptr)
        {
            return head;
        }
        else
        {
            ListNode* newHead = reverseList02(head->next);
            head->next->next = head;
            head->next = nullptr;
            return newHead;
        }
    }
};

void test01()
{
    Solution s;
    ListNode* list1 = new ListNode(0, new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4)))));
    printList(list1);
    printList(s.reverseList01(list1));
    ListNode* list2 = new ListNode(5, new ListNode(6, new ListNode(7, new ListNode(8, new ListNode(9)))));
    printList(list2);
    printList(s.reverseList02(list2));
}

int main()
{
    test01();

    system("pause");
    return 0;
}

Python版本
class ListNode:
    def __init__(self, x):
        self.val = x     # 节点值
        self.next = None # 后继节点引用
        
def print_ListNode(node):
    while node:
        print(node.val, end=' ')
        node = node.next
    print()    
    
class Solution:
    def reverseList01(self, head):
        cur = head
        pre = None
        if not head or not head.next:
            return head
        while cur:
            tmp = cur.next # 暂存后继节点 cur.next
            cur.next = pre # 修改 next 引用指向
            pre = cur      # pre 暂存 cur
            cur = tmp      # cur 访问下一节点
        return pre
 
    def reverseList02(self, head):
        if head == None or head.next == None:
            return head
        #继续递归后一个节点
        newHead = self.reverseList02(head.next)
        #反转节点
        head.next.next = head
        head.next = None
        return newHead

def test01():
    solution = Solution()
    # 实例化节点
    n1 = ListNode(1) # 节点 head
    n2 = ListNode(2)
    n3 = ListNode(3)
    n4 = ListNode(4)
    n5 = ListNode(5)

    # 构建引用指向
    n1.next = n2
    n2.next = n3
    n3.next = n4
    n4.next = n5
    print_ListNode(n1)

    # print_ListNode(solution.reverseList01(n1))
    print_ListNode(solution.reverseList02(n1))
            
if __name__=="__main__":
    test01()

Java版本
package com.hailei_01;

public class reverseList {
    public static void main(String[] args) {
        ListNode list1 = new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5)))));
        printListNode(list1);
        printListNode(reverseList01(list1));
        ListNode list2 = new ListNode(6,new ListNode(7,new ListNode(8,new ListNode(9,new ListNode(10)))));
        printListNode(list2);
        printListNode(reverseList02(list2));
    }

    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; };
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public static ListNode reverseList01(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode cur = head; // 指向头结点
        ListNode pre = null; // 空
        while (cur != null) {
            ListNode temp = cur.next; // 暂存后继节点
            cur.next = pre;   // 修改next引用指向
            pre = cur;        // 暂存
            cur = temp;    // 访问下一节点
        }
        return pre;
    }

    public static ListNode reverseList02(ListNode head) {
        return recur(head, null);    // 调用递归并返回
    }

    public static ListNode recur(ListNode cur, ListNode pre) {
        if (cur == null) return pre; // 终止条件
        ListNode res = recur(cur.next, cur);  // 递归后继节点
        cur.next = pre;              // 修改节点引用指向
        return res;                  // 返回反转链表的头节点
    }

    public static void printListNode(ListNode headNode) {
        ListNode listNode = headNode;
        while(listNode!=null) {
            System.out.print(listNode.val+" ");
            listNode = listNode.next;
        }
        System.out.println();
    }
}

希望本文对大家有帮助,上文若有不妥之处,欢迎指正

分享决定高度,学习拉开差距

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/960507.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号