[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字符串非空判断 " ...
随机推荐
- Dropbox + Farbox高速创建免费博客小站
创建自己的Dropbox账号(已有账号的略过) 注冊地址:Dropbox 点击链接注冊就好了,so easy: 账号注冊成功后,能够选择下载同步client(windows.Mac.ios.andro ...
- 安装Linux_[CentOS]系统
Lunx操作系统安装 虚拟机:VirtualBox. 操作系统的选择:CentOS 6.3. (64位/32位都可). (CentOS:诞生于社区的企业级操作系统). Install Or Upgra ...
- 国际化之MessageFormat与占位符
如果一个字符串文本中包含了多个与国际化相关的数据,可以使用MessageFormat类对这些数据进行批量处理. 例如: 在2016年1月9日的时候,一场台风导致了500间房屋的摧毁和¥1000000元 ...
- Servlet的学习之Session(1)
在学习完了Servlet中的Cookie技术后,我们再来学习另一个能保存会话数据的技术——Session. Session是服务器端技术,利用这个技术,服务器在运行时可以为每一个用户的浏览器创建一个其 ...
- 高斯消元法~get√
高斯消元法,是线性代数中的一个算法,可用来求解线性方程组,并可以求出矩阵的秩,以及求出可逆方阵的逆矩阵.高斯消元法的原理是:若用初等行变换将增广矩阵 化为 ,则AX = B与CX = D是同解方程组. ...
- android图片压缩的3种方法实例
android 图片压缩方法: 第一:质量压缩法: private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = ...
- ALV编辑行内容有改变时候操作
ALV编辑行内容时,调用方法 check_changed_data返回变量 gf_valid = 'X'的话说明alv行有变化. 以下拿alv维护表程序部分代码做例: DATA: gr_alvgrid ...
- 构建基于Javascript的移动web CMS——模板
在上一篇<构建基于Javascript的移动CMS--Hello,World>讲述了墨颀 CMS的大概组成,并进行了一个简单的演示样例,即Hello,World.这一次,我们将把CMS简单 ...
- Android自定义shape的使用
MainActivity如下: package cn.testshape; import android.os.Bundle; import android.app.Activity; /** * D ...
- arch Failed to load module "intel"
arch启动x的时候出现问题困扰我一天了,终于解决掉了. 错误如下: [ 61.086] (II) LoadModule: "intel" [ 61.087] (WW) Warni ...