【数据结构】做题笔记--区间反转链表

简介: 【数据结构】做题笔记--区间反转链表

牛客的一道题,区间反转链表,请大佬指点

/**
 * struct ListNode {
 *  int val;
 *  struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @param m int整型 
 * @param n int整型 
 * @return ListNode类
 */
#include <stdio.h>
#include <stdlib.h>
struct ListNode* reverseBetween(struct ListNode* head, int m, int n ) {
    // write code here
    if(m==n)
    return head;
    struct ListNode *g,*p,*h;
    g=p=NULL;
    h=head;
    struct ListNode *w=(struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode *now=(struct ListNode*)malloc(sizeof(struct ListNode));
    now=head;
    while (now->val!=m) {
    now=head->next;
    head=head->next;
    }
    w=head;
    while (n!=w->val) {
    w=head->next;
    head=head->next;
    }
    g=h;
    if(w->next!=NULL){
        g->next=w;
    }
    g=w->next;
    while(now!=w){
        p=now->next;
        now->next=g;
        g=now;
        now=p;
 
    }
    now->next=g;
    if(h->next!=NULL)
    return h;
    return now;
}
相关文章
|
14天前
|
存储
数据结构第二课 -----线性表之单向链表
数据结构第二课 -----线性表之单向链表
|
5天前
|
存储 算法 Java
数据结构与算法 数组和链表
数据结构与算法 数组和链表
11 0
|
5天前
|
存储 Java
深入浅出数据结构之链表
深入浅出数据结构之链表
|
5天前
|
C++
数据结构(双链表
数据结构(双链表
8 1
|
8天前
|
存储 缓存
[数据结构]~双向+循环链表从(0~1)
[数据结构]~双向+循环链表从(0~1)
|
14天前
|
存储
数据结构第三课 -----线性表之双向链表
数据结构第三课 -----线性表之双向链表
|
15天前
|
存储 Java
数据结构奇妙旅程之顺序表和链表
数据结构奇妙旅程之顺序表和链表
|
19天前
|
存储 C语言
数据结构基础:双链表结构、实现
数据结构基础:双链表结构、实现
|
19天前
|
存储
数据结构基础:一篇文章教你单链表(头插,尾插,查找,头删等的解析和代码)
数据结构基础:一篇文章教你单链表(头插,尾插,查找,头删等的解析和代码)
|
22天前
|
C语言
数据结构:5、链表之双向链表
数据结构:5、链表之双向链表
25 0