Reverse a singly linked list

Source

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
 
typedef struct Node {
    char data;
    struct Node* next;
} Node;
 
void print_list(Node* root) {
    while (root) {
        printf("%c ", root->data);
        root = root->next;
    }
    printf("\n");
}
 
Node* reverse(Node* root) {
    Node* new_root = 0;
    while (root) {
        Node* next = root->next;
        root->next = new_root;
        new_root = root;
        root = next;
    }
    return new_root;
}
 
int main() {
    Node d = { 'd', 0 };
    Node c = { 'c', &d };
    Node b = { 'b', &c };
    Node a = { 'a', &b };
 
    Node* root = &a;
    print_list(root);
    root = reverse(root);
    print_list(root);
 
    return 0;
}

[轉]Reverse a singly linked list的更多相关文章

  1. LeetCode 206 Reverse a singly linked list.

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  2. Reverse a singly linked list

    Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...

  3. [cc150] check palindrome of a singly linked list

    Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse ...

  4. 单链表反转(Singly Linked Lists in Java)

    单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法   package dsa.linkedlist; public class Node<E> ...

  5. [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点

    Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...

  6. Singly Linked List

    Singly Linked List Singly linked list storage structure:typedef struct Node{ ElemType data; struct N ...

  7. Reverse a Singly LinkedList

    Reverse a Singly LinkedList * Definition for singly-linked list. * public class ListNode { * int val ...

  8. [TS] Implement a singly linked list in TypeScript

    In a singly linked list each node in the list stores the contents of the node and a reference (or po ...

  9. amazon o2 - reverse second half linked list

    public ListNode reverseBetween(ListNode head, int m, int n) { if(head==null) return head; ListNode s ...

随机推荐

  1. apache 安装配置 (centos)

    1. 使用yum包安装Apache软件 [root@Apache ~]# yum -y install httpd* [root@Apache ~]# rpm -qa | grep httpd --查 ...

  2. f-strings格式化输出

    python3.6后加入标准库的格式化输出新的写法 不区分大小写,f,F都可以 可以加入表达式 s1 = 'haha' s = f'python{s1.upper()}' l1 = ['小明', 18 ...

  3. [fw]IDT表的初始化

    IDT表的初始化  linux内核的中断描述符表IDT是一个全局的数据,在i386平台上被定义为: struct desc_struct idt_table[256] __attribute__((_ ...

  4. c# 自定义控件之 ComboBox

    winform 自带的 combobox 无法支持根据输入文本匹配列表中的项目,需要使用自定义控件. public class MyCombobox : ComboBox { //初始化数据项 pri ...

  5. CentOS使用手册(三)

    前言: 目录:暂时不写,因为有些实验,比如负载均衡,反向代理,配置ssl等实验来不及做.所以这篇随笔还需日后补充(排版以后慢慢调,现在该做平台分析系统了) Linux中Mongodb4.x安装调试.远 ...

  6. setclock - 用系统时间来设置硬件时间

    总览 setclock 描述 setclock 用当前系统时钟存储的时间设置系统的硬件时间. 它先读取 /etc/sysconfig/clock 的时间格式, 时间存储前应该转化成这种格式. Red ...

  7. 西里尔字 俄语 - Cyrillic

    https://zh.wikipedia.org/wiki/%E8%A5%BF%E9%87%8C%E5%B0%94%E5%AD%97%E6%AF%8D 其他编码[编辑] 其他适用西里尔字母的字符编码系 ...

  8. Qt 浅析Q_PROPERTY宏

    最近在使用QProperAnimation画类,研究这个的时候看到别人写的代码有用到 Q_PROPERTY()这个宏,然后查了下,这个宏只有Qt才有的 并且需要进行编译,继承于QOBJECT Qt 手 ...

  9. Mysql中存储过程和函数的写法

    MySQL中,创建存储过程的基本形式如下: CREATE PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic ...] routine ...

  10. Springboot解决使用@Scheduled创建任务时无法在同一时间执行多个任务的BUG

    1.在启动类使用 @SpringBootApplication @EnableJpaRepositories(repositoryFactoryBeanClass = MyRepositoryFact ...