[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);
}
[Leetcode]-ReverseLinkedList的更多相关文章
- leetcode — reverse-linked-list
/** * Source : https://leetcode.com/problems/reverse-linked-list/ * * * Reverse a singly linked list ...
- 【LeetCode题解】206_反转链表(Reverse-Linked-List)
目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以访问我的 git ...
- 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 on ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- LeetCode 206 单链表翻转
https://leetcode.com/problems/reverse-linked-list/ 思路很简单,分别设置三个结点,之后依次调整结点1和结点2的指向关系. Before: pre -& ...
- Leetcode 题解
Leetcode Solutions Language: javascript c mysql Last updated: 2019-01-04 https://github.com/nusr/lee ...
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
- LeetCode链表解题模板
一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...
- leetcode 刷题记录(java)-持续更新
最新更新时间 11:22:29 8. String to Integer (atoi) public static int myAtoi(String str) { // 1字符串非空判断 " ...
随机推荐
- STM32的FSMC总线复用调试笔记
调试FSMC总线复用模式时主要遇到以下几点: 1.寄存器的配置,首先注意使能地址数据复用,其次要存储器类型选择FSMC_MemoryType_NOR,否则出现不了NADV信号. FSMC_NORSRA ...
- winform基础——数据访问及几个案例
数据访问分为三个部分:(1)创建链接(2)创建与执行命令(3)读取或准备相关数据 一,需要引用的命名空间 using data: using data.SqlClient; 二,创建与数据库的链接—— ...
- Windows 8 动手实验系列教程 实验7:磁贴和通知
动手实验 实验7:磁贴和通知 2012年9月 简介 磁贴是Windows应用商店应用用户体验的重要元素.当应用程序被安装后,它的磁贴将在Windows 8开始屏幕被创建.该磁贴(称为主磁贴)作为启动应 ...
- spring mvc ModelAndView向前台传值
今天在做项目的时候遇到一个问题,把第一个页面保存的id传到第三个页面中去用,原来是在controller层加了一个全局变量控制的,但是后来发现这个变量实现不了我要的功能,于是查了一下,原来ModelA ...
- Google Ads Encryption Key
aes | floyd's Google Ads Encryption Key
- NetBeans + Xdebug 调试WordPress
用NetBeans进行WordPress的相关开发和定制很顺手,配合Xdebug后调试起来也很方便. 详细配置过程如下(本例中Xampp安装目录为D:\xampp): 1: 下载xdebug(版本需匹 ...
- 【IACV】边缘检测技术传统的方法与理论
1.边缘检测的目的 边缘检测是图像分析中使用到的最常见的操作之一,而且相比其他任何主题来说,文献中提到的与边缘增强(edge enhancement)[1]与边缘检测(edge detection)[ ...
- 轻量级工具网站SimpleTools
[解释]本来这篇文章是在前天发出来的,可是当时是刚申请的域名,现在都要域名实名认证,导致我发的项目网址打不开,惹来了很多博友的吐槽,在此说声抱歉,今天一大早就把实名认证提交了,现在网站已经可以正常访问 ...
- MFC实现多风格真彩色大图标工具栏按钮
研究zlib库,想实现一个类似winrar功能的小东东,打开winrar界面看它的工具栏比较好看于是动手想做一个,当然资源也使用的是winrar附带的.下面是截图:真彩色(32位)32*32大图标工具 ...
- delphi 回调函数
program Project2; {$APPTYPE CONSOLE} uses SysUtils; type //定义一个对象事件方法 TCallbackFunc = function (i: I ...