美文网首页
单向链表反转 2022-02-22 周二

单向链表反转 2022-02-22 周二

作者: 松哥888 | 来源:发表于2022-02-22 21:12 被阅读0次

基本思路

  • 准备1:判空,head为空(无节点),直接返回空指针;

  • 准备2:Head为输入指针,最好不要修改它,所以用一个指针,current代替它

  • 准备3:两个辅助节点:previous(反转后的新head),next(暂存下一个节点)

  • 准备4:结束条件current 为空,这个时候,previous就是反转后的头节点,返回就好。

  • 步骤1:保存next指针
    next = current.next;

  • 步骤2:反转
    current.next = previous;

  • 步骤3:移动到下一个
    previous = current;
    current = next;

  • 返回:previous

C代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* reverseList(struct ListNode* head) {
    // 判空
    if (head == NULL) {
        return NULL;
    }

    // 三个指针
    struct ListNode *current = head;
    struct ListNode *previous = NULL;
    struct ListNode *next = NULL;
    
    // 反转
    while(current != NULL) {
        next = current->next;
        current->next = previous;
        previous = current;
        current = next;
    }
    
    // 返回反转后的新头
    return previous;
}

JS代码

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    // 判空
    if (!head) {
        return null;
    }

    // 三个指针
    let previous = null;
    let current = head;
    let next = null;

    // 反转
    while (current) {
    // 保存next
    next = current.next;
    // 替换next
    current.next = previous;
    // 设置pre的值
    previous = current;
    // 设置当前项的值
    current = next;
    }
      
    return previous;
};

Swift代码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init() { self.val = 0; self.next = nil; }
 *     public init(_ val: Int) { self.val = val; self.next = nil; }
 *     public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
 * }
 */
class Solution {
    func reverseList(_ head: ListNode?) -> ListNode? {
        // 判空
        if head == nil {
            return nil
        }

        // 三个指针
        var current = head
        var previous : ListNode? = nil
        var next : ListNode? = nil

        // 反转
        while current != nil {
            next = current?.next
            current?.next = previous
            previous = current
            current = next
        }

        // 返回新的head
        return previous
    }
}

简单比较

  • swift和JavaScript的代码真的很像

  • 语法上JavaScript随意一些,当然代码安全差距很大(编译语言和脚本语言)

  • 性能上C > Swift > JavaScript

性能差距

参考文章

看一遍就理解,图解单链表反转

C代码

JS代码

Swift代码

相关文章

  • 单向链表反转 2022-02-22 周二

    基本思路 准备1:判空,head为空(无节点),直接返回空指针; 准备2:Head为输入指针,最好不要修改它,所以...

  • 数据结构 - 单向链表及相关算法

    单向链表 链表常见算法 链表反转

  • reverse linked list

    反转单向链表 demo 运行效果

  • 数据结构专题:1.单向链表反转与排序

    有如下单向链表 1.单向链表反转,递归 递归的方式其实是从尾节点开始进行指针反转,最终递归反转到头节点 2.单向链...

  • 单向链表算法

    单向链表 反转单向链表 单链表查找倒数第k个节点 单链表递归倒序打印 单链表排序 单链表删除重复节点

  • 数据结构-链表

    相关掌握点 单向链表 双向链表 反转单向链表 判断链表是否含有环 链表构建 链表是一种线性结构,是通过指针引用节点...

  • 单向链表-链表反转

    最近在并行复习数据结构与算法的知识,为了加强掌握,就把做题思路用画图的方式记录下来。今天是第一篇,常见的问题:链表...

  • 链表 - 单向链表反转

    反转一个单链表。输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL 图解...

  • 链表常见问题

    反转单向链表思路使用一个临时节点当做缓冲节点,通过绕圈完成反转 反转部分链表题:给定一个链表,及反转的范围,完成反...

  • 单向链表反转

    https://blog.csdn.net/blioo/article/details/62050967

网友评论

      本文标题:单向链表反转 2022-02-22 周二

      本文链接:https://www.haomeiwen.com/subject/uewglrtx.html