算法---链表

文章目录

  • 反转链表
  • 合并两个有序链表
  • 删除重复元素

反转链表

反转链表包括两种,反转全部元素或者反转部分元素。在这里,我们约定:数据元素类型是struct LinkNode,要反转链表的第一个节点是head,head的前面一个节点是pre,如果head是首节点,则pre等于NULL,要反转链表的最后一个节点的后一个节点是p。请添加图片描述
比如说我们要反转的是2,5,4,3,则head节点是2,pre是7,p是6;如果反转的是9,7,2,则head是9,pre是NULL,p是3.

下面函数返回值是反转链表后的首节点,head是要反转链表的首节点,p是要反转链表的最后一个节点的后一个节点。

struct LinkNode *reverse(struct LinkNode *head,struct LinkNode*p)
{struct LinkNode *pre = p;while(head!=p){struct LinkNode *next = head->next;head->next=pre;pre = head;head=next;}return pre;
}

LeedCode 206. 反转链表反转全部元素,反转全部元素最后一个节点的下一个节点是NULL。

struct ListNode* reverse(struct ListNode* head,struct ListNode *p){struct ListNode *pre=p;while(head!=pre &&head!=NULL){struct ListNode *next = head->next;head->next=pre;pre=head;head=next;}return pre; 
}struct ListNode* reverseList(struct ListNode* head){return reverse(head,NULL);
}

请添加图片描述
LeedCode 92. 反转链表 II反转部分链表。

struct ListNode* reverseList(struct ListNode* head,struct ListNode *p){struct ListNode *pre = p;struct ListNode *curr = head;while(head!=p){struct ListNode *next = head->next;head->next = pre;pre = head;head = next;}return pre;
}/**
找到要反转链表的首节点以及最后一个节点的后一个节点。
*/
struct ListNode* reverseBetween(struct ListNode* head, int left, int right){struct ListNode *pre = NULL;//pre:要反转链表的前一个元素struct ListNode *curr = head;//curr:要反转链表的最后一个元素for(int i=1;i<left;i++){pre = curr;curr = curr->next; }for(int i=left;i<right;i++){curr = curr->next;}/*如果pre不等于NULL,说明pre的下一个节点是首节点,如果pre等于NULL,head就是要反转的首节点,反转链表最后一个节点的后一个节点是curr->next*/if(pre!=NULL){pre->next = reverseList(pre->next,curr->next);return head;}else{return reverseList(head,curr->next);}
}

合并两个有序链表

21. 合并两个有序链表
使用递归思路。

/*
函数返回值是两个链表按照递增合并后的链表
*/
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){if(list1==NULL){return list2;}if(list2==NULL){return list1;}if(list1->val <list2->val){list1->next = mergeTwoLists(list1->next,list2);return list1;}else{list2->next = mergeTwoLists(list1,list2->next);return list2;}
}

删除重复元素

struct ListNode* deleteDuplicates(struct ListNode* head){if(head==NULL || head->next==NULL){return head;}//head = head->val==head->next->val?head->next:head;head->next = deleteDuplicates(head->next);return head->val==head->next->val?head->next:head;
}

请添加图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379479.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SSM

二、环境设置&#xff08;MyEclipse&#xff09; 1&#xff0c;字体设置 window–>Preference->General->Appearance->Colors and Fonts->Basic Text->Font 2&#xff0c;workspace字符集设置 window–>Preference->General->Appearance->W…

IOS NSArray,NSDictionary

小结&#xff1a; NSArray有序的集合&#xff1b; NSDictionary无序的集合&#xff0c;可排序&#xff1b; 增删改查 ------NSArray----------- create : 1)NSArray *array [NSArray arrayWithObjects:"Henry","Jones", "Susan", "Smith&q…

Java PropertyPermission equals()方法与示例

PropertyPermission类equals()方法 (PropertyPermission Class equals() method) equals() method is available in java.util package. equals()方法在java.util包中可用。 equals() method is used to check whether this object and the given object (ob) are equal or not…

c#配合oracle快速导入excel方法--原创(6万条记录5分钟左右)

原理&#xff1a;用c#采用读取Excel数据源方式将数据读入c#的datatable,循环datatable,将datatable中的数据用stringbuilder拼成insert into (字段名) valus (值);每5条插入一个符号&#xff08;作用是将sql字符串限制在4000字符以内&#xff09;&#xff0c;然后将拼成的字符串…

English最俗语法大全

一、先分析两个长难句 1,It is a truth universally acknowledged that a single man in possession of a good fortune must be in want of a wife. 人们公认这样一个事实&#xff0c;一个有钱的单身男人一定想要娶一个妻子。 in want of want 想要 university widely 广泛的…

tfs 内网和外网切换的方法。

C:\Windows\System32\drivers\etc的hosts文件配置一个123.67.128.109 geo-dept-3转载于:https://www.cnblogs.com/lwflt/archive/2012/07/23/2604731.html

observable_Java Observable countObservers()方法与示例

observable可观察的类countObservers()方法 (Observable Class countObservers() method) countObservers() method is available in java.util package. countObservers()方法在java.util包中可用。 countObservers() method is used to count the number of observers exists…

设计模式--Strategy 策略模式

所谓策略模式(Strategy Pattern)&#xff0c;就是将策略 (算法) 封装为一个对象&#xff0c;易于相互替换&#xff0c;如同 USB 设备一样可即插即用&#xff1b;如果将策略、具体的算法和行为&#xff0c;编码在某个类或客户程序内部&#xff0c;将导至事后的修改和扩展不易。 …

HDU-1518 Square dfs+剪枝

该题问给定的棍子能否组成一个正方形。首先我们要判定是否总长度是4的倍数&#xff0c;然后再决定是否存在某条边大于组合边长。 搜索的过程中也是可以进行剪枝了。 首先将边排序&#xff0c;我们可以假定所有的组合边由大小递减的边组成&#xff0c;那么我们在搜索的时候就不用…

英语思维黄金法则

一、谓语单一原则 英文的句子当中&#xff0c;有且只有一套谓语结构。 要想使用多个谓语&#xff0c;有以下三种方法&#xff1a; 1&#xff0c;利用连词将不同谓语并列起来 2&#xff0c;把其中的一些动词给降级&#xff08;v-ing v-ed 非谓语动词&#xff09; 3&#xff0c;…

java getname_Java文件类字符串getName()方法(带示例)

java getname文件类字符串getName() (File Class String getName()) This method is available in package java.io.File.getName(). 软件包java.io.File.getName()中提供了此方法。 This method is used to retrieve or return the filename or directory name and represente…

WF中DependencyObject和DependencyProperty的实现

WF中DependencyObject和DependencyProperty的实现 DependencyProperty的Register和RegisterAttached方法&#xff0c;将DependencyProperty存在IDictionary中完成注册&#xff0c;确保相同name的DependencyProperty在一个ownerType类型中只能有一个。 DependencyObject的GetVal…

hdu2115: I Love This Game

hdu2115: http://acm.hdu.edu.cn/showproblem.php?pid2115题意&#xff1a;输入n组名字和对应的时间&#xff08;分&#xff1a;秒&#xff09;&#xff0c;要求按时间长度由短到长排序&#xff0c;并输出对应排名&#xff0c;若时间一样&#xff0c;则按名字字典序排序&#…

打开eclipse出现Failed to load the JNI shared library “D:\java\jdk\bin\...\jre\bin\server\jvm.dll”如何解决?

eclipse打开的时候出现Failed to load the JNI shared library “D:\java\jdk\bin…\jre\bin\server\jvm.dll”如何解决&#xff1f;&#xff1f; 如图所示&#xff1a; 即代表你的jdk与eclipse的位数不一样&#xff01;&#xff01;&#xff01; 你可以查看一下eclipse和jd…

Java DataOutputStream writeUTF()方法及示例

DataOutputStream类的writeUTF()方法 (DataOutputStream Class writeUTF() method) writeUTF() method is available in java.io package. writeUTF()方法在java.io包中可用。 writeUTF() method is used to write the given string value to the basic data output stream wit…

2010年世界杯分组

A 南非 墨西哥 乌拉圭 法国 B 阿根廷 南非 韩国 希腊 C 英格兰 美国 阿尔及利亚 斯洛文尼亚 D 德国 澳大利亚 塞尔维亚 加纳 E 荷兰 丹麦 日本 喀麦隆 F 意大利 巴拉圭 新西兰 斯洛伐克 G 巴西 朝鲜 科特迪瓦 葡萄牙 H 西班牙 瑞士 洪都拉斯 智利 转载于:https://www.cnblogs.c…

圆形坠落模拟算法设计

目标&#xff1a;实现一个算法&#xff0c;模拟在一个封闭二维区域&#xff0c;圆形小球朝给定方向坠落的过程&#xff0c;实现二维区域的紧密填充。 像下面这样&#xff1a; 难点&#xff0c;及其简单解决&#xff1a; 1.如何把粒子移动尽可能远&#xff1f; 图中的粒子i&…

Maven详细教学

一、Maven简介 maven&#xff1a;是apache下的一个开源项目&#xff0c;是纯java开发&#xff0c;并且只是用来管理java项目的 依赖管理&#xff1a;就是对jar包的统一管理 可以节省空间 项目一键构建&#xff1a;mvn tomcat:run该代码可以将一个完整的项目运行起来&#xff0…

Java Character.UnicodeBlock of()方法与示例

Character.UnicodeBlock类的()方法 (Character.UnicodeBlock Class of() method) of() method is available in java.lang package. of()方法在java.lang包中可用。 of() method is used to return the Unicode block containing the given parameter value or it returns null…

simpleDBM的B-link树实现

参考的是VLDB2005的这篇论文&#xff0c;做个标记把。/Files/YFYkuner/Concurrency_control_and_recovery_for_balanced_B-link_trees.pdf 转载于:https://www.cnblogs.com/YFYkuner/archive/2009/12/21/1629268.html