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. Sql 将A表数据插入到B表

    A表和B表字段不同 --insert into B(Name,PersonalId,Education,IsDel) select Name, PersonId as PersonalId, ( ca ...

  2. for循环(C语言型)举例

  3. Ansible--01 ansible基础 Ansible-ad- hoc

    目录 自动化运维工具-Ansible基础 自动化运维的含义 Ansible 基础及安装 Ansible的架构 Ansible的执行流程 ansible配置文件 ansible Inventory(主机 ...

  4. 转:谈谈Linux下动态库查找路径的问题

    http://blog.chinaunix.net/uid-23069658-id-4028681.html 学习到了一个阶段之后,就需要不断的总结.沉淀.清零,然后才能继续“上路”.回想起自己当年刚 ...

  5. 最长上升子序列(LIS)长度及其数量

    例题51Nod-1376,一个经典问题,给出一个序列问该序列的LIS以及LIS的数量. 这里我学习了两种解法,思路和代码都是参考这两位大佬的: https://www.cnblogs.com/reve ...

  6. window常见操作

    cmd查看进程命令 nestat -aon|findstr "端口" 杀进程: taskkill /f /pid 端口号  /f参数强制杀进程 通过注册表删除桌面图标 cmd re ...

  7. InnoDB索引存储结构

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11429438.html InnoDB默认创建的主键索引是聚簇索引(Clustered Index),其 ...

  8. vue.js axios使用

    1. 自定义配置 /** * Created by superman on 17/2/16. * http配置 */ import axios from 'axios' import utils fr ...

  9. C++ 排序(未完)

    参考: 快速排序 堆排序 各类排序 #include <iostream> #include <vector> #include <time.h> #include ...

  10. shell读取文件第一行和最后一行,小数的运算比较

    1. 读取文件的第一行:head -n +1 file.txt 读取文件的最后一行: tail -n -1 file.txt echo 12:30:55 | cut -d: -f 1 结果为12,意思 ...