LintCode:链表操作(合并与反转)
描述:
(1)翻转一个链表
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null
**************************************************************************
(2)将两个排序链表合并为一个新的排序链表
给出 1->3->8->11->15->null,2->null, 返回 1->2->3->8->11->15->null。
解题思路:
已经定义好ListNode链表类,翻转可以采用栈(stack)来进行读取,也可以采用三个指针来变换,还可以采用递归的方式。
排序就要用到两个指针,头指针和尾指针,尾指针进行数据的指引操作,最后返回头指针即可,也可以采用递归的方式。
代码如下:
import java.util.Stack;
public class 翻转链表 {
/**
* Definition for ListNode.
*/
public class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
/*
* @param head: n
*
* @return: The new head of reversed linked list.
*/
public ListNode reverse(ListNode head) {
// write your code here
if (head == null)
return null;
ListNode reverseHead = new ListNode(0);
ListNode current = reverseHead;
Stack<ListNode> st = new Stack<ListNode>();//定义栈
while (head != null) {
st.push(head); //压入栈中
head = head.next;
}
while (!st.empty()) {
current.next = st.pop(); //取出节点
current = current.next;
}
current.next = null;
return reverseHead.next;
}
/**
* @param head: The head of linked list.
* @return: The new head of reversed linked list.
*/
public ListNode reverse2(ListNode head) {
// write your code here
ListNode revHead = null;
ListNode prev = null;
while(head!=null){
ListNode pNext = head.next;
if(pNext == null){
// 翻转后的头节点,后面是空,结果
revHead = head;
}
// head的下一个节点指向之前要链接的节点
head.next = prev;
// 要链接的节点 直线p节点,以供下次链接
prev = head;
// head节点更新,指向pNext
head = pNext;
}
return revHead;
}
/**
* 递归
* @param head
* @return
*/
public ListNode reverse3(ListNode head) {
// write your code here
if( head ==null || head.next ==null)
return head;
ListNode second = head.next;
head.next = null;
ListNode res = reverse(second);
second.next = head;
return res;
}
}
public class 合并两个排序链表 {
/**
* Definition for ListNode.
*/
public class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
/**
* @param ListNode
* l1 is the head of the linked list
* @param ListNode
* l2 is the head of the linked list
* @return: ListNode head of linked list
*/
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
// write your code here
if (l1 == null && l2 != null)
return l2;
if (l1 != null && l2 == null)
return l1;
if (l1 == null && l2 == null)
return null;
ListNode head = new ListNode(0); //头指针
ListNode current = head; //尾指针
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
current.next = l1;
current = current.next;
l1 = l1.next;
} else {
current.next = l2;
current = current.next;
l2 = l2.next;
}
}
if (l1 != null)
current.next = l1;
if (l2 != null)
current.next = l2;
return head.next;
}
/**
* 递归方法
* @param l1
* @param l2
* @return
*/
public ListNode mergeTwoLists2(ListNode l1, ListNode l2) {
// write your code here
if(l1==null && l2!=null)
return l2;
if(l1!=null && l2==null)
return l1;
if(l1==null && l2==null)
return null;
ListNode MergeHead = null;
if(l1.val < l2.val){
MergeHead = l1;
MergeHead.next = mergeTwoLists(l1.next,l2);
}else{
MergeHead = l2;
MergeHead.next = mergeTwoLists(l1,l2.next);
}
return MergeHead;
}
}
LintCode:链表操作(合并与反转)的更多相关文章
- C/C++链表操作(面试)
1.为了反转这个单链表,我们先让头结点的next域指向结点2,再让结点1的next域指向结点3,最后将结点2的next域指向结点1,就完成了第一次交换,顺序就变成了Header-结点2-结点1-结点3 ...
- bzoj 1483 [HNOI2009]梦幻布丁(链表+启发式合并)
1483: [HNOI2009]梦幻布丁 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1818 Solved: 761[Submit][Status ...
- linux内核里的字符串转换 ,链表操作常用函数(转)
1.对双向链表的具体操作如下: list_add ———向链表添加一个条目 list_add_tail ———添加一个条目到链表尾部 __list_del_entry ———从链表中删除相应的条目 l ...
- C#的链表操作[数据结构-线性表]
链式存储结构图解: 上图中,a1,是数据,紧跟着后面的d1是下一个节点的地址值.也就是一个节点的最后存储的是下一个节点的地址值,最后一个节点的存储的下一个地址值是null,代表链表结束. 1,定义链表 ...
- linux 内核的链表操作(好文不得不转)
以下全部来自于http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 无任何个人意见. 本文详细分析了 2.6.x 内 ...
- JAVA 链表操作:循环链表
主要分析示例: 一.循环链表简述 二.单链表循环链表 三.双链表循环链表 一.循环链表简述 循环链表即链表形成了一个循环的结构,尾节点不再指向NULL,而是指向头节点HEAD,此时判定链表的结束是尾节 ...
- 单链表操作B 分类: 链表 2015-06-07 12:42 15人阅读 评论(0) 收藏
数据结构上机测试2-2:单链表操作B TimeLimit: 1000ms Memory limit: 65536K 题目描述 按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除 ...
- YTU 2620: B 链表操作
2620: B 链表操作 时间限制: 1 Sec 内存限制: 128 MB 提交: 418 解决: 261 题目描述 (1)编写一个函数createlink,用来建立一个动态链表(链表中的节点个数 ...
- C# 链表操作
关于链表操作,在C#当中微软已经提供了一个LinkedList<T>的数据结构,通过这个类提供的一系列方法就能够实现链表操作. 这里我提供一段代码,这是在论坛里面有人提问时给出的代码,它实 ...
- C语言,单链表操作(增删改查)(version 0.1)
这天要面试,提前把链表操作重新写了一遍.备份一下,以备不时之需. 希望有人能看到这篇代码,并指正. // File Name : list.h #include "stdafx.h" ...
随机推荐
- shell脚本检测网络是否畅通
shell初始化安装脚本执行时,需从网络上安装一些rpm包,所有需要先检测网络的畅通性, 代码 #检测网络链接&&ftp上传数据 function networkAndFtp() { ...
- static 关键字的使用,静态和非静态类的区别
直接以一个例子说明: using System; using System.Collections.Generic; using System.Diagnostics; using System.IO ...
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】
[030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...
- linux下拷贝隐藏文件
1.拷贝隐藏文件 把/home/u文件夹中的全部文件(包含隐藏文件)拷贝到/home/user1中 cp -a /home/u/. /home/user1 2.改动主机名: vi /e ...
- MySQL二:数据库操作
阅读目录 一 知识储备 二 初识SQL语言 三 系统数据库 四 创建数据库 五 数据库相关操作 一 知识储备 MySQL数据库基本操作知识储备 数据库服务器:一台计算机(对内存要求比较高) 数据库管理 ...
- html+JS刷图实现视频效果
网页播放视频须要载入播放器,可是通过刷图也能实现视频播放的效果 JS中用到Z-index属性,记录一篇解说Z-index属性的博客的地址: http://www.cnblogs.com/gisdrea ...
- Spring Boot从入门到实战:整合通用Mapper简化单表操作
数据库访问是web应用必不可少的部分.现今最常用的数据库ORM框架有Hibernate与Mybatis,Hibernate貌似在传统IT企业用的较多,而Mybatis则在互联网企业应用较多.通用Map ...
- lua学习笔记(九)
环境 全局变量table lua把所有的全局变量存在一个table里,并把这个table赋值给一个全局变量_G _G也在这个全局变量的table里,它就是一个普通 ...
- linux 参数内核
优化Linux内核参数 转自:http://www.centoscn.com/CentOS/config/2013/0804/992.html vim /etc/sysctl.conf 1.net ...
- linux下LAMP环境搭建
++++++++++++++++++++++++++++++++++++++++++++++ linux下LAMP环境搭建 ++++++++++++++++++++++++++++++++++++++ ...