LeetCode刷题--点滴记录009

9. 剑指 Offer 24. 反转链表

要求

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

解题

C++版本
#include <iostream>
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();
    }
}

在这里插入图片描述

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲁棒最小二乘支持向量机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值