算法与数据结构面试题(10)-颠倒链表
2014-12-19 11:15
309 查看
题目
用一种算法来颠倒一个链接表的顺序。现在在不用递归式的情况下做一遍。解题思路
1.先用递归颠倒2.尝试不用递归颠倒
代码
1.递归式
public class Problem8 {public LinkedListNode invert(LinkedListNode node) {
if (node == null) {
throw new NullPointerException();
}
LinkedListNode endNode = null;
LinkedListNode nextNode = node.getNextNode();
if (nextNode != null) {
node.setNextNode(null);
endNode = invert(nextNode);
nextNode.setNextNode(node);
} else {
endNode = node;
}
return endNode;
}
public LinkedListNode parseLinkedList(int[] data) {
LinkedListNode lastNode = null;
for (int i = data.length - 1; i >= 0; i--) {
LinkedListNode currentNode = new LinkedListNode();
currentNode.setValue(data[i]);
currentNode.setNextNode(lastNode);
lastNode = currentNode;
}
return lastNode;
}
public static void main(String[] args) {
int[] data = { 1, 2, 3, 4, 5, 6, 7 };
Problem8 problem8 = new Problem8();
LinkedListNode rootNode = problem8.parseLinkedList(data);
LinkedListNode endNode = problem8.invert(rootNode);
problem8.printlnLinkedArray(endNode);
System.out.println("Done");
}
public void printlnLinkedArray(LinkedListNode node){
if(node == null) return;
System.out.println("" + node.getValue());
printlnLinkedArray(node.getNextNode());
}
}
输出
7
6
5
4
3
2
1
Done
2.非递归式
public class Problem8_2 {public LinkedListNode invert(LinkedListNode node, int length) {
LinkedListNode[] nodes = new LinkedListNode[length];
// 先断链
for (int i = 0; i < length; i++) {
if (node != null) {
nodes[i] = node;
node = node.getNextNode();
}
}
// 再指针反向
for (int i = length - 1; i >= 0; i--) {
if (i == 0) {
nodes[i].setNextNode(null);
} else {
nodes[i].setNextNode(nodes[i - 1]);
}
}
return nodes[length - 1];
}
public LinkedListNode parseLinkedList(int[] data) {
LinkedListNode lastNode = null;
for (int i = data.length - 1; i >= 0; i--) {
LinkedListNode currentNode = new LinkedListNode();
currentNode.setValue(data[i]);
currentNode.setNextNode(lastNode);
lastNode = currentNode;
}
return lastNode;
}
public static void main(String[] args) {
int[] data = { 1, 2, 3, 4, 5, 6, 7 };
Problem8_2 problem8 = new Problem8_2();
LinkedListNode rootNode = problem8.parseLinkedList(data);
LinkedListNode endNode = problem8.invert(rootNode, data.length);
problem8.printlnLinkedArray(endNode);
System.out.println("Done");
}
public void printlnLinkedArray(LinkedListNode node) {
if (node == null)
return;
System.out.println("" + node.getValue());
printlnLinkedArray(node.getNextNode());
}
}
相关文章推荐
- 转:链表相交有环 经典面试题(三)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 算法与数据结构面试题(9)-颠倒字符串
- 算法与数据结构面试题(1)-把二元查找树转变成排序的双向链表
- 《Delphi 算法与数据结构》学习与感悟[10]: 双向链表
- 数据结构学习笔记 --- 线性表 (一些常见的关于链表的算法和面试题)
- 线性表11|单链表小结:腾讯面试题 - 数据结构和算法16
- 算法与数据结构面试题(13)-求链表倒数第K个节点
- 《算法大全-面试题-链表-栈-二叉树-数据结构》
- 数据结构面试题总结10——链表:链表综合
- 数据结构学习笔记 --- 线性表 (一些常见的关于链表的算法和面试题)
- 算法与数据结构学习 10 链表操作
- 算法与数据结构面试题(7)-链表“香蕉”问题
- 算法与数据结构面试题(11)-一次遍历得到链表的中间节点
- 线性表11|单链表小结:腾讯面试题 - 数据结构和算法16
- 微软等数据结构+算法面试100题(10)--三道面试题
- 经典面试题(一)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 数据结构之线性链表的算法
- 新手学习数据结构与算法---链表归并
- 经典面试题(四)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
- 经典面试题(三)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯