• #力扣:206. 反转链表@FDDLC


    206. 反转链表

    一、Java

    1. class Solution {
    2. public ListNode reverseList(ListNode head) {
    3. if (head == null) return null;
    4. ListNode pre = null, cur = head, next = head.next;
    5. while (next != null) {
    6. cur.next = pre;
    7. pre = cur;
    8. cur = next;
    9. next = cur.next;
    10. }
    11. cur.next = pre;
    12. return cur;
    13. }
    14. }
    1. class Solution {
    2. public ListNode reverseList(ListNode head) {
    3. ListNode pre = null, cur = head, next;
    4. while (cur != null) {
    5. next = cur.next;
    6. cur.next = pre;
    7. pre = cur;
    8. cur = next;
    9. }
    10. return pre;
    11. }
    12. }
    1. class Solution {
    2. public ListNode reverseList(ListNode head) {
    3. if (head == null || head.next == null) return head;
    4. ListNode next = head.next, newHead = reverseList(next);
    5. head.next = null;
    6. next.next = head;
    7. return newHead;
    8. }
    9. }
    1. class Solution {
    2. public ListNode reverseList(ListNode head) {
    3. if (head == null || head.next == null) return head;
    4. ListNode newHead = reverseList(head.next);
    5. head.next.next = head;
    6. head.next = null;
    7. return newHead;
    8. }
    9. }

    二、C++

    1. class Solution {
    2. public:
    3. ListNode* reverseList(ListNode* head) {
    4. ListNode *pre = nullptr, *cur = head, *next;
    5. while (cur != nullptr) {
    6. next = cur->next;
    7. cur->next = pre;
    8. pre = cur;
    9. cur = next;
    10. }
    11. return pre;
    12. }
    13. };
    1. class Solution {
    2. public:
    3. ListNode* reverseList(ListNode* head) {
    4. if (head == nullptr || head->next == nullptr) return head;
    5. ListNode *newHead = reverseList(head->next);
    6. head->next->next = head;
    7. head->next = nullptr;
    8. return newHead;
    9. }
    10. };

    三、Python

    1. class Solution:
    2. def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
    3. pre, cur, next = None, head, None
    4. while cur is not None:
    5. next = cur.next
    6. cur.next = pre
    7. pre = cur
    8. cur = next
    9. return pre
    1. class Solution:
    2. def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
    3. if head is None or head.next is None:
    4. return head
    5. newHead = self.reverseList(head.next)
    6. head.next.next = head
    7. head.next = None
    8. return newHead

    四、JavaScript

    1. var reverseList = function (head) {
    2. let pre = null, cur = head, next;
    3. while (cur != null) {
    4. next = cur.next;
    5. cur.next = pre;
    6. pre = cur;
    7. cur = next;
    8. }
    9. return pre;
    10. };
    1. var reverseList = function (head) {
    2. if (head == null || head.next == null) return head;
    3. let newHead = reverseList(head.next);
    4. head.next.next = head;
    5. head.next = null;
    6. return newHead;
    7. };

    五、Go

    1. func reverseList(head *ListNode) *ListNode {
    2. var pre *ListNode
    3. cur := head
    4. for cur != nil {
    5. next := cur.Next
    6. cur.Next = pre
    7. pre = cur
    8. cur = next
    9. }
    10. return pre
    11. }
    1. func reverseList(head *ListNode) *ListNode {
    2. if head == nil || head.Next == nil {
    3. return head
    4. }
    5. newHead := reverseList(head.Next)
    6. head.Next.Next = head
    7. head.Next = nil
    8. return newHead
    9. }

  • 相关阅读:
    对业务分析中常见智能化设备的梳理(20220803)
    Hadoop的UI页面介绍
    T8161B T8403 T8448 ICS TRIPLEX 具有支持物联网边缘的计算机视觉
    数据驱动!精细化运营!用机器学习做客户生命周期与价值预估!
    使用Response的验证码案例_黑马javaweb案例
    Dart 2.17 正式发布
    Spring IoC容器初始化过程
    JDBC 版本和历史
    BUUCTF [BJDCTF2020]藏藏藏 1
    Linux修改远程登陆端口
  • 原文地址:https://blog.csdn.net/liuxc324/article/details/133757109