[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字符串非空判断 " ...
随机推荐
- SRM 627 D1L2GraphInversionsDFS查找指定长度的所有路径 Binary indexed tree (BIT)
题目:http://community.topcoder.com/stat?c=problem_statement&pm=13275&rd=16008 由于图中边数不多,选择DFS遍历 ...
- JS - 图片放大器
下载地址:http://www.lanrentuku.com/js/tupian-1170.html
- PHP开发-上传文件
<?php /****************************************************************************** 参数说明: $max_ ...
- heap表和iot表排序规则不同
heap 和iot 对比 OBJECT_NAME OBJECT_TYPE --------------------------------------------------------------- ...
- JSTL解析——005——core标签库04
直接入主题,标签讲解 1.<c:import>标签 JSP里面有<% file include="XX"%> 与<jsp:include>,JS ...
- Swift - 创建代理协议实现页面间参数传递和方法调用
在开发中,经常需要用到协议代理模式.比如,进入编辑页面修改数据后,将新数据回传到主界面. 下面通过一个样例来说明协议代理模式,功能如下: 1,主页面有一个标签和一个修改按钮,点击修改按钮会跳转到编辑页 ...
- Java反射库中的安全漏洞在30个月后终于修复了(转)
2013年7月,安全组织Security Explorations发现了Java 7u25中的一个安全漏洞,通过这个漏洞攻击者可以完全摆脱Java沙箱.Oracle在更新的7u40中包含了一个补丁,但 ...
- Python天天美味(25) - 深入理解yield
Python天天美味(25) - 深入理解yield - CoderZh - 博客园 Python天天美味(25) - 深入理解yield yield的英文单词意思是生产,刚接触Python的时候 ...
- windows下搭建node.js及npm的工作环境
近期在研究数据可视化D3框架,决定在windows下搭建一个nodejs及npm的工作环境,在网上查了n篇文章,别管是编译源代码安装也好.还是使用node.msi格式安装包也好,总是有问题.终于,功夫 ...
- 简单深刻:为控件创建MouseEnter和MouseLeave事件(覆盖WndProc,增加对消息的处理,真简单!)——连对CM_MOUSEENTER的消息处理都是颇有深意啊!
其实很简单: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, D ...