20231205-Day 8-点亮代码技能

Leetcode206(代码随想录:反转链表)

C++代码:

C++定义链表结构体:

//Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

在链表结构体中定义构造函数可以在初始化结点时直接为结点赋值,避免产生空指针异常。

双指针方法:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        //双指针法
        ListNode *pre = nullptr;    //必须初始化为空
        ListNode *cur = head;
        while(cur)
        {
            ListNode *temp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
};
递归方法:
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        // ListNode *pre = nullptr;
        return reverse(nullptr, head);
    }
    ListNode *reverse(ListNode *pre, ListNode *cur){
        if(cur == nullptr)
            return pre;
        ListNode *temp = cur->next;
        cur->next = pre;
        return reverse(cur, temp);
    }
};

Python代码:

1. python求数组nums长度:

        len(nums)

2. python取整:

        middle = (left + right) // 2

3. python移除数组nums中的元素val:

        nums.remove(val)

4.(1)python升序排序,列表本身修改:

                nums.sort()

   (2)python升序排序,返回新列表:

                sorted(nums)

5.(1)列表地址相同:result = nums

   (2)创建列表副本:result = nums[:]

6.(1)float('inf'):正无穷大

   (2)float('-inf'):负无穷大

7. 建立二维列表的方法:result = [[0] * n for _ in range(n)]

8. Python中的空类型:None

9. 在类中:

   (1)成员函数互相调用使用:self.函数名

   (2)调用其他类的函数使用:类名.函数名

Python定义链表类:

# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
双指针方法:
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        cur = head
        pre = None
        while cur:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        return pre
递归方法:
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        return self.reverse(head, None)
        
    def reverse(self, cur: ListNode, pre: ListNode) -> ListNode:
        if cur == None:
            return pre
        temp = cur.next
        cur.next = pre
        return self.reverse(temp, cur)

  • 35
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值