link 链表反转

链表反转,示例如下:

偶数个输入:a->b->c->d->e->f

偶数个输出:e->f->c->d->a->b

or

奇数个输入:a->b->c->d->e->f->g

偶数个输入:g->e->f->c->d->a->b

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

/************** start of stack *************/

#define STACK_SIZE 1024

char stack[STACK_SIZE];
int top = 0;

void push(char ch){
    stack[top] = ch;
    top++;
}

char pop(){
    top--;
    return stack[top];
}

int isempty(){
    return 0 == top;
}

void test_stack(){
    push(‘a‘);
    push(‘b‘);
    push(‘c‘);
    push(‘d‘);

    while(!isempty()){
        printf("pop ch: %c\n", pop());
    }
}

/************** end of stack *************/

struct _node{
    char data;
    struct _node *next;
};

typedef struct _node node, *plink;

plink init_link(){
    plink pl;
    pl = (plink)malloc(sizeof(node));

    // check malloc success or not
    if(NULL == pl) {
        printf("malloc memory fail...");
        return NULL;
    }

    // init link head
    pl->data = ‘\0‘;
    pl->next = NULL;

    return pl;
}

void input_data(plink pl, char data){
    plink p = pl;

    while(p->next){
        p = p->next;
    }

    plink node = NULL;
    node = (plink)malloc(sizeof(node));     // malloc a new node

    // add data
    if(NULL != node){
        node->data = data;
        node->next = p->next;        // last next is NULL
        p->next = node;
        p = node;                    // p point last node
    }
}

void output_link(plink pl){
    if(NULL == pl){
        printf("plink is null");
        return;
    }

    plink p = pl->next;    // already check pl is NULL, so here is ok
    while(NULL != p){
        printf("%c -> ", p->data);
        p = p->next;
    }
    printf("\n\n");
}

// push and pop stack
plink revert_link2(plink pl){
    plink p = pl;

    while(p->next){
//        printf("p->data: %c\n", p->next->data);
        if(p->next->next){
            push(p->next->next->data);
            push(p->next->data);
            p = p->next->next;
        } else {
            push(p->next->data);
            p = p->next;
        }
    }

    while(!isempty()){
        printf("%c -> ", pop());
    }

    printf("\n\n");

    return NULL;
}

plink revert_link(plink pl){
    if(NULL == pl){    // check link is NULL
        return NULL;
    }

    int link_len = 0;
    plink tmp_pl = pl->next;

    while(tmp_pl){    // count link count
        link_len++;
        tmp_pl = tmp_pl->next;
    }

    // link length is no more than two node(s)
    if(link_len <= 2){
        return pl;
    }

    // link length is more than two nodes
    return revert_link2(pl);
}

int main(){
    plink pl = NULL;

    pl = init_link();         // init link head

    input_data(pl, ‘a‘);      // add data
    input_data(pl, ‘b‘);
    input_data(pl, ‘c‘);
    input_data(pl, ‘d‘);
    input_data(pl, ‘e‘);
    input_data(pl, ‘f‘);
    input_data(pl, ‘g‘);

    output_link(pl);

    plink pl2 = revert_link(pl);

    output_link(pl2);

    return 0;
}

/****
revert_link.c

linux gcc compile
gcc revert_link.c -o revert_link && ./revert_link

output result:

a -> b -> c -> d -> e -> f -> g
g -> e -> f -> c -> d -> a -> b

or

a -> b -> c -> d -> e -> f
e -> f -> c -> d -> a -> b

****/

间隔螺旋反转:

输入: a -> b -> c -> d -> e -> f 
输出: b -> a -> d -> c -> f -> e

plink revert_link3(plink pl){
    if(NULL == pl){
        printf("plink is null");
        return NULL;
    }	

    plink p = pl;
    plink first = p->next;
    while(NULL != first){
        plink second = first->next;
        if(NULL != second){
            first->next = second->next;     // third node
            second->next = first;           // revert two nodes
            first = first->next;
            p->next = second;
            p = second->next;
        }
    }
    return pl;
}
时间: 2024-08-24 18:07:53

link 链表反转的相关文章

单链表反转问题

单链表反转问题 基本问题 如何将单链表反转? 算法实现 /** * * Description: 单链表反转. * * @param head * @return ListNode */ public static ListNode reverseList(ListNode head) { if (head == null) { return head; } ListNode prev = null; ListNode current = head; ListNode next = null;

链表反转的递归和非递归实现方式

链表反转是数据结构的基本功,主要有递归和非递归两种实现方式.我们一一介绍如下: 1. 非递归实现 主要包括如下4步: 1)如果head为空,或者只有head这一个节点,return head即可: 2)从头到尾遍历链表,把reversedHead赋值给当前节点的next: 3)当前节点赋值给reversedHead: 4)遍历结束,return reversedHead. 下图试图来辅助说明: 代码如下: node* reverseList(node* head) { if(head == NU

链表反转(递归方式,非递归方式)

//非递归方式进行链表反转 public ListNode reverseList(ListNode head){ if(head==null||head.next==null){ return head; }else { ListNode pre=head; ListNode p=head.next; ListNode next=null; while (p!=null) { next=p.next; p.next=pre; pre=p; p=next; } head.next=null; r

单链表反转的2种方法

1 public class ReverseDemo { 2 3 /** 4 * 单链表反转的两种方法 5 */ 6 public static void main(String[] args) { 7 Node head =new Node("a"); 8 Node node2=new Node("b"); 9 Node node3=new Node("c"); 10 head.setNext(node2); 11 node2.setNext(

链表 单链表反转

思路1:O(n^2). “狸猫换太子”,不进行改动链表结构,只首尾交换len/2次.但是在本函数中用到了定位函数,定位函数实际上是遍历了一遍整个链表,所以综合效率很低,达到O(n^2). //单链表反转(O(n^2)) void reverseList(Node* Head) { int count = numOfNodes(Head); //首尾交换 for(int i=1; i<=count/2; i++) { Node* p1 = locateNodeI(Head, i); Node* p

C++将链表反转的实现

有题目的需求是求将链表反转,例如1->2->3->4->5转变成5->4->3->2->1,经典的是可以有两种解决方法,递归方式和非递归方式,下面给出C++的这两周实现过程. #include<iostream> using namespace std; const int N = 6; typedef int DataType;//定义数据类型 typedef struct node{//创建链表node DataType data; stru

单向链表反转算法——递归版和迭代版

最近在做笔试题时,遇到一道编程题:单向链表反转算法.一时紧张,没写出来就提前交卷了,然而交完卷就想出来了... 最初想出来的是递归版,遗憾的是没能做到尾递归,后来又琢磨出了迭代版.后来用实际编译运行测试了一遍,能正常运行. 递归版的灵感来源于<Haskell 趣学指南>中非常简洁的快速排序算法的实现,其思想是将单向链表分割头部和尾部.其中头部指是链表的第一个节点,尾部是指除去第一个节点后的子链表.通过递归的方法,将子链表继续分割成头部和尾部,直至尾部指剩下一个节点,无法继续分割,然后将头部和尾

链表反转leetcode206

最近准备结束自己的科研生涯,准备要开始找工作了,准备在LEETCODE刷刷题...刷的前40题全部用python刷的,各种调包速度奇快,后被师哥告知这样没意义,于是准备开始回归C++,Python用的多了再用C++总是忘记敲分号和括号,甚至Compile Error了几次 = =.尴尬 链表反转比较简单,首先用自己的"本科"方法做了一下,发现效率并不高: 1 class Solution { 2 public: 3 ListNode* reverseList(ListNode* hea

数据结构——线性表——链表反转

链表反转有两种常见方式.下面从图中详细解释.其中带有部分核心代码,最后上完整代码. 迭代法 //首先定义三个变量    PNODE pre = NULL;    PNODE now = pHead->pNext;    PNODE next = NULL; next = now->pNext;//先保存下一个节点 now->pNext = pre;//当前节点的next指针指向前一个节点 pre = now;//前一个节点赋值为当前节点 now = next;//当前节点赋值为下一个节点