[Leetcode]-ReverseLinkedList

题目:单链表取反

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

typedef struct node *list;
typedef struct node *position;
typedef struct node *ListNode;

typedef struct node
{
    int data;
    position next;
}node;

static list init_list(void);
static void delete_list(list L);
static int isempty(list L);
static void insert_node(position L,int data);
static void delete_node(list L,position P);
static position find_last(list L);
static position find_value(list L,int data);
static position find_pre(list L,position P );
static void print(list L);

list init_list(void){
    list L  = (list)malloc(sizeof(node));
    L->next = NULL;
    return L;
}
void delete_list(list L){
    position P ,next;
    P = L;
    do{
        next = P->next;
        free(P);
        P = next;
    }while(next != NULL);
}
int isempty(list L){
    return (L->next == NULL);
}
void insert_node(position P,int data){
    position tem ;
    tem = (position)malloc(sizeof(node));
    tem->data = data;
    tem->next = P->next;
    P->next = tem;
}
void delete_node(list L,position P){
    position pre ;
    pre = find_pre( L, P);
    if(pre != NULL)
    {
        pre->next = P->next;
        free(P);
    }
    else
    {
        printf("delete_node:p is not in the list!\n");
    }

}
position find_last(list L){
    position P;
    P=L;
    while(P->next != NULL)
    {
        P = P->next;
    }
    return P;

}
position find_value(list L,int data){
    position P ;
    P = L;
    while(P->next != NULL)
    {
        P = P->next;
        if(P->data == data)
            return P;
    }
    return NULL;
}

position find_pre(list L,position P ){
    position tem ;
    tem = L;
    while(tem->next != NULL)
    {
        if(tem->next == P)
            return tem;
        tem = tem->next;
    }
    return NULL;
}
void print(list L){
    position P;

    if(isempty( L))
    {
        printf("print: L is a null list!\n");
        return ;
    }
    P = L;
    while(P->next !=NULL)
    {
        P = P->next;
        printf("print:%p : %d \n",P,P->data);
    }
    printf("\n");
}

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
ListNode reverseList( ListNode head) {

    if((head == NULL)||(head->next == NULL) )
        return head;
    ListNode  cur= head;
    ListNode  tempt=NULL;

    while (cur->next!=NULL&& cur!=NULL)
    {
        tempt=cur->next;
        cur->next=tempt->next;
        tempt->next=head;
        head=tempt;
        tempt=cur->next;
    }

    return head;
}

int main()
{
    int a[6]= {1,2,3,4,5,6};
    int i=0;
    list L,L1;
    L = init_list();

    print(L);
    printf("insert node\n");
    for(i=0;i<6;i++)
    {
        insert_node( L,a[i]);
    }
    print( L);
    L1 = reverseList(L);
    print( L1);
}
时间: 2024-10-09 10:51:04

[Leetcode]-ReverseLinkedList的相关文章

Leetcode解题-链表(2.2.2)ReverseLinkedList

题目:2.2.2 Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->nullptr, m = 2 and n = 4, return 1->4->3->2->5->nullptr. Note: Given m, n satisfy t

leetcode 链表题总结

按照frequency来排序,总共27题 1.2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/#/description 两个链表相加,注意就是进位问题. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } *

【Leetcode】Reverse Linked List

原题链接:https://leetcode.com/problems/reverse-linked-list/ 题目: Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 思路: 头插法建立链表 算法: [java] view plain copy public ListNode reverseL

[LeetCode][JavaScript]Reverse Linked List

Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? https://leetcode.com/problems/reverse-linked-list/ 反转链表. 1 /** 2 * Definition

LeetCode链表解题模板

一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改,比如要计算链表的长度: ListNode* p = head; int num = 0; while(p!=NULL){ num++; p = p->next; } 如果要找到最后的节点,可以更改while循环中的条件,只不过需要加上head为NULL时的判断 if(!head) return hea

每天一道面试题LeetCode 206 -- 反转链表

LeetCode206 反转链表 思路 代码 # # @lc app=leetcode.cn id=206 lang=python3 # # [206] 反转链表 # # https://leetcode-cn.com/problems/reverse-linked-list/description/ # # algorithms # Easy (61.53%) # Likes: 624 # Dislikes: 0 # Total Accepted: 112.8K # Total Submiss

LeetCode刷题 --基础知识篇-- 链表

题目来源与力扣,传送门在这里. 众所周知,链表是很重要的一种数据结构,但同时也很容易出错,二狗在重温这部分内容时被人指点了一些典型的题目,顺手去leetCode刷了一些,记录如下. <206.单链表反转>(https://leetcode-cn.com/problems/reverse-linked-list/) 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL 1

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,