[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字符串非空判断 " ...
随机推荐
- paip.php 配置ZEND DEBUGGER 断点调试for cli..
paip.php 配置ZENDDEBUGGER 断点调试for cli.. 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http:/ ...
- 【Nginx笔记】nginx配置文件具体解释
本文主要对nginx的配置做重点说明,关于nginx的其他基本概念.建议參考官网描写叙述.这里推荐Nginx Beginner's Guide这篇文档.对刚開始学习的人高速认识nginx非常有帮助. ...
- Servlet的学习之Session(3)
在上一篇<Servlet的学习之Session(2)>我们知道了Session能实现一个会话过程中保存数据或者多个会话中实现同一个Session的关键因素就是Cookie,只是Cookie ...
- Spark Sreaming与MLlib机器学习
Spark Sreaming与MLlib机器学习 本来这篇是准备5.15更的,但是上周一直在忙签证和工作的事,没时间就推迟了,现在终于有时间来写写Learning Spark最后一部分内容了. 第10 ...
- ExtJS拖拽效果
ExtJS拖拽效果 <html> <head> <title>hello</title> <meta http-equiv="conte ...
- Android菜鸟的成长笔记(7)——什么是Activity
原文:[置顶] Android菜鸟的成长笔记(7)——什么是Activity 前面我们做了一个小例子,在分析代码的时候我们提到了Activity,那么什么是Activity呢? Activity是An ...
- Windows 8 和 Windows 8.1 中对插件和 ActiveX 的支持
此文章将介绍页面在 Windows 8 适用于桌面版的 Internet Explorer 中与在新 Windows UI 的 Internet Explorer 中的不同表现. Windows 8 ...
- html ui设计案例
1.jquery特效:http://www.5icool.org 2. http://www.open-lib.com/Lib/1992.jsp
- 为HttpStatusCodeResult加入customErrors
asp.net mvc的action返回值为HttpStatusCodeResult时的customErrors总是不起作用 (404和exception时的500,因为他们并不是HttpStatus ...
- android打包apk时混淆遇到的问题
android打包apk的时候一般会选择混淆,而在eclipse中常使用的是proguard来混淆.有很多时候引用了第三方包的时候会导致打包不成功,或者打包成功不能运行的情况. 首先看看正常的prog ...