剑指Offer之Java算法习题精讲链表与二叉树专项训练
作者:明天一定. 时间:2022-01-12 19:19:01
题目一
链表题——反转链表
根据单链表的头节点head来返回反转后的链表
具体题目如下
解法
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre,cur,nxt;
pre = null;
cur = head;
nxt = head;
while(cur!=null){
nxt = cur.next;
cur.next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
}
题目二
链表题——反转链表
按照一定数量的节点来进行反转并返回反转之后的链表
具体题目如下
解法
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null) return null;
ListNode a, b;
a = b = head;
for (int i = 0; i < k; i++) {
if (b == null) return head;
b = b.next;
}
ListNode newHead = reverse(a, b);
a.next = reverseKGroup(b, k);
return newHead;
}
ListNode reverse(ListNode a, ListNode b) {
ListNode pre,cur,nxt;
pre = null;
cur = a;
nxt = a;
while(cur!=b){
nxt = cur.next;
cur.next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
}
题目三
链表题——回文链表
根据单链表的头节点head来判断该链表是否是回文链表,并返回结果
具体题目如下
解法:后序遍历与left比较
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
ListNode left;
public boolean isPalindrome(ListNode head) {
left = head;
return traverse(head);
}
boolean traverse(ListNode right){
if (right == null) return true;
boolean res = traverse(right.next);
res = res && (right.val == left.val);
left = left.next;
return res;
}
}
题目四
二叉树题——翻转二叉树
根据所给的二叉树根节点root来翻转此二叉树,并返回翻转后的二叉树根节点
具体题目如下
解法
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return null;
}
TreeNode lf = invertTree(root.left);
TreeNode rg = invertTree(root.right);
root.left = rg;
root.right = lf;
return root;
}
}
题目五
二叉树题——填充节点
给定一个完美二叉树,填充该二叉树每个节点的下一个右侧节点指针
具体题目如下
解法
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
if(root==null) return null;
method(root.left,root.right);
return root;
}
public void method(Node left,Node right){
if (left == null || right == null) {
return;
}
left.next = right;
method(left.left,left.right);
method(right.left,right.right);
method(left.right,right.left);
}
}
题目六
二叉树链表题——将二叉树展开为链表
根据给定的二叉树根节点root,将此二叉树展开为单链表
具体题目如下
解法
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public void flatten(TreeNode root) {
if (root == null) return;
flatten(root.left);
flatten(root.right);
TreeNode left = root.left;
TreeNode right = root.right;
root.left = null;
root.right = left;
TreeNode p = root;
while (p.right != null) {
p = p.right;
}
p.right = right;
}
}
来源:https://blog.csdn.net/wai_58934/article/details/123031314
标签:Java,链表,二叉树
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android studio实现简单的计算器
2022-09-07 23:23:28
![](https://img.aspxhome.com/file/2023/3/98623_0s.jpg)
JAVA抛出异常的三种形式详解
2022-06-26 22:44:32
android实现可自由移动、监听点击事件的悬浮窗
2022-04-12 14:15:31
Spring中ApplicationContextAware的使用方法详解
2023-12-25 07:01:33
如何用120行Java代码写一个自己的区块链
2023-07-17 03:44:33
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
2021-06-01 14:28:00
![](https://img.aspxhome.com/file/2023/3/66253_0s.jpg)
unity实现虚拟摇杆控制Virtual Joystick
2022-09-26 11:56:28
![](https://img.aspxhome.com/file/2023/3/77263_0s.jpg)
android实现文本复制到剪切板功能(ClipboardManager)
2023-11-28 17:40:31
java中带参数的try(){}语法含义详解
2021-10-27 05:20:16
![](https://img.aspxhome.com/file/2023/1/76211_0s.jpg)
详解JavaWeb中的 Listener
2023-09-04 08:09:24
![](https://img.aspxhome.com/file/2023/5/110835_0s.png)
Java编程中使用XFire框架调用WebService程序接口
2023-11-06 20:16:33
![](https://img.aspxhome.com/file/2023/0/58990_0s.png)
Java实现图片拼接
2023-02-28 23:01:27
超详细的Intellij IDEA 看源码必备技能
2021-10-27 09:03:16
![](https://img.aspxhome.com/file/2023/2/69312_0s.png)
ToLua框架下C#与Lua代码的互调操作
2023-12-07 06:27:48
![](https://img.aspxhome.com/file/2023/7/67567_0s.jpg)
图形学之Unity渲染管线流程分析
2023-09-25 05:27:36
![](https://img.aspxhome.com/file/2023/9/87029_0s.jpg)
Java实现添加文字水印&图片水印的方法详解
2023-01-28 08:11:39
![](https://img.aspxhome.com/file/2023/5/110795_0s.png)
Java中的几种读取properties配置文件的方式
2022-09-06 13:36:19
mybatis框架xml下trim中的prefix与suffix等标签的用法
2023-09-20 18:55:24
Android自定义TextView实现drawableLeft内容居中
2022-04-24 18:20:14
![](https://img.aspxhome.com/file/2023/2/122092_0s.png)
Room Kotlin API的使用入门教程
2023-11-07 07:06:35