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. c# 使用网站的身份验证及 Cookie 的获取与使用

    C# 的 Http 访问可以使用 .net 自带的  HttpWebRequest, WebClient, HttpClient 类.也可以使用开源库 RestSharp . RestSharp 的优 ...

  2. VUE $SET源码

  3. js运算符的优先级的顺序列表

    优先级权重 运算符 17 ..[].new 16 () 15 ++.-- 14 !.~.+(单目).-(单目).typeof.void.delete 13 %.*./ 12 +(双目).-(双目) 1 ...

  4. 第八组Postmortem事后分析

    第八组Postmortem事后分析 一.团队成员总结的改进和教训 隆晋威:Beta阶段完善架构设计,分工更加明确,文档更丰富,交流带来开销减少.Alpha技术选型不固定,分工混乱,没有方便的测试引擎, ...

  5. 2019-11-7-WPF-How-to-get-plain-text-from-RichTextBox

    title author date CreateTime categories WPF How to get plain text from RichTextBox lindexi 2019-11-0 ...

  6. gcc 交叉工具链中工具使用(arm-linux-xxx)

    在Ubuntu系统中使用 gcc 系列工具是在PC机上使用 arm-linux-gcc 编译的目标 是在 arm CPU上使用 一.安装交叉编译工具链 1. 编译工具怎么获取 1)从官网 http:/ ...

  7. dotNET面试(三)

    1.简述 private. protected. public. internal 修饰符的访问权限.private : 私有成员, 在类的内部才可以访问 ,也就是类内部的函数等成员可以访问.prot ...

  8. 利用Kubernetes(K8S)部署JAVA项目

    一.jar包和war包区别 首先简单介绍一下jar包和war包区别,当时就没分清,导致部署总是傻傻分不清楚. jar包:jar包就是java的类进行编译生成的class文件就行打包的压缩包.里面是一些 ...

  9. Nginx Web 基础入门

    目录 Nginx Web 基础入门 Nginx快速安装 两种方式部署Nginx 如何升级nginx或者添加功能 使用systemd管理nginx nginx相关配置文件 nginx的配置文件详解 虚拟 ...

  10. IDEA使用Maven搭建JavaWeb项目

    1. 新建项目 2. 填写项目坐标信息 3. 手动生效pom.xml 4. 添加maven常用配置 1) 设置项目统一编码 <!-- 设置编码 --> <properties> ...