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. [Leetcode] 176.第二高薪水

    题目: 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 | ...

  2. 常用缓存淘汰算法(LFU、LRU、ARC、FIFO、MRU)

    缓存算法是指令的一个明细表,用于决定缓存系统中哪些数据应该被删去. 常见类型包括LFU.LRU.ARC.FIFO.MRU. 最不经常使用算法(LFU): 这个缓存算法使用一个计数器来记录条目被访问的频 ...

  3. javascript基础六(事件对象)

    1.事件驱动     js控制页面的行为是由事件驱动的.          什么是事件?(怎么发生的)     事件就是js侦测到用户的操作或是页面上的一些行为       事件源(发生在谁身上)   ...

  4. setquota - 设置磁盘配额或时间限制

    SYNOPSIS(总览) setquota [ -u | -g ] filesystem-name block-soft block-hard inode-soft inode-hard name.. ...

  5. Codeforces 1188C DP 鸽巢原理

    题意:定义一个序列的beauty值为序列中元素之差绝对值的最小值,现在给你一个数组,问所有长度为k的子序列的beauty值的和是多少? 思路:(官方题解)我们先解决这个问题的子问题:我们可以求出bea ...

  6. centos系统jdk安装

    下载Oracle官网的jdk来安装 不使用openjdk 最新的官网地址: https://www.oracle.com/technetwork/java/javase/downloads/jdk8- ...

  7. CNN基础二:使用预训练网络提取图像特征

    上一节中,我们采用了一个自定义的网络结构,从头开始训练猫狗大战分类器,最终在使用图像增强的方式下得到了82%的验证准确率.但是,想要将深度学习应用于小型图像数据集,通常不会贸然采用复杂网络并且从头开始 ...

  8. sass-loader屡次安装不生效的问题

    报错信息: npm WARN sass-loader@8.0.0 requires a peer of webpack@^4.36.0 but none is installed. You must ...

  9. boost scope exit

    Boost.ScopeExit provides the macro BOOST_SCOPE_EXIT, which can be used to define something that look ...

  10. 【Flutter学习】可滚动组件之滚动监听及控制

    一,概述 ScrollController可以用来控制可滚动widget的滚动位置 二,ScrollController 构造函数 ScrollController({ double initialS ...