使用Java实现反转链表源码
时间: 2023-03-16 13:44:17 浏览: 80
可以使用以下Java代码实现反转链表: public class ReverseLinkedList {
static class Node {
int val;
Node next;
Node(int val) {
this.val = val;
}
}
public static Node reverseLinkedList(Node head) {
Node prev = null;
Node current = head;
while(current != null) {
Node next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
}
相关问题
java实现双向链表(附完整源码)
下面是Java实现双向链表的完整源码:
```java
public class DoublyLinkedList<T> {
private Node head;
private Node tail;
private int size;
public DoublyLinkedList() {
head = null;
tail = null;
size = 0;
}
private class Node {
T data;
Node prev;
Node next;
public Node(T data) {
this.data = data;
prev = null;
next = null;
}
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void addFirst(T data) {
Node newNode = new Node(data);
if (isEmpty()) {
tail = newNode;
} else {
head.prev = newNode;
newNode.next = head;
}
head = newNode;
size++;
}
public void addLast(T data) {
Node newNode = new Node(data);
if (isEmpty()) {
head = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
}
tail = newNode;
size++;
}
public void add(int index, T data) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Invalid index!");
}
if (index == 0) {
addFirst(data);
return;
}
if (index == size) {
addLast(data);
return;
}
Node current = head;
for (int i = 0; i < index - 1; i++) {
current = current.next;
}
Node newNode = new Node(data);
newNode.prev = current;
newNode.next = current.next;
current.next.prev = newNode;
current.next = newNode;
size++;
}
public T removeFirst() {
if (isEmpty()) {
throw new RuntimeException("Empty list!");
}
T data = head.data;
head = head.next;
if (head == null) {
tail = null;
} else {
head.prev = null;
}
size--;
return data;
}
public T removeLast() {
if (isEmpty()) {
throw new RuntimeException("Empty list!");
}
T data = tail.data;
tail = tail.prev;
if (tail == null) {
head = null;
} else {
tail.next = null;
}
size--;
return data;
}
public T remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Invalid index!");
}
if (index == 0) {
return removeFirst();
}
if (index == size - 1) {
return removeLast();
}
Node current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
current.prev.next = current.next;
current.next.prev = current.prev;
size--;
return current.data;
}
public T get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Invalid index!");
}
Node current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current.data;
}
public void set(int index, T data) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Invalid index!");
}
Node current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
current.data = data;
}
public boolean contains(T data) {
Node current = head;
while (current != null) {
if (current.data.equals(data)) {
return true;
}
current = current.next;
}
return false;
}
public void clear() {
head = null;
tail = null;
size = 0;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
Node current = head;
while (current != null) {
sb.append(current.data);
if (current.next != null) {
sb.append(", ");
}
current = current.next;
}
sb.append("]");
return sb.toString();
}
}
```
代码中使用了嵌套类Node表示双向链表中的每个节点,包含一个数据域和两个指针域(prev和next),分别指向前驱节点和后继节点。
程序首先定义了一个DoublyLinkedList类,包含头节点head、尾节点tail和链表长度size三个属性。然后,程序实现了以下方法:
- size:返回链表长度
- isEmpty:判断链表是否为空
- addFirst:在链表头部插入一个元素
- addLast:在链表尾部插入一个元素
- add:在指定位置插入一个元素
- removeFirst:删除链表头部元素
- removeLast:删除链表尾部元素
- remove:删除指定位置的元素
- get:获取指定位置的元素
- set:设置指定位置的元素
- contains:判断链表中是否包含指定元素
- clear:清空链表
- toString:将链表转换为字符串形式
以上是Java实现双向链表的完整源码,希望能对你有所帮助。
java实现俄罗斯方块源码
使用Java实现俄罗斯方块源码需要先了解游戏的逻辑和规则。俄罗斯方块游戏是一个经典的益智游戏,玩家需要操作不同形状的方块,使它们在游戏区域内进行旋转、移动和下落,以堆积出完整的水平线,当一行或多行被填满时,这些行将被清除,给玩家得分。
在Java中实现俄罗斯方块,首先需要创建方块的类,用来存储方块的形状、位置和状态,并实现方块的旋转、移动和下落等操作。接着需要创建游戏区域的类,用来管理游戏区域的大小、状态和显示,以及处理方块的下落、堆积和消除等逻辑。
另外,还需要实现游戏的控制器类,用来接收玩家的操作输入,并对方块进行旋转、移动和下落操作,同时处理游戏的得分和游戏结束等逻辑。最后,还需要实现游戏界面的类,用来显示游戏区域、方块和得分等信息,并接收玩家的操作输入。
总的来说,使用Java实现俄罗斯方块的源码需要通过类和对象的设计来管理游戏的逻辑和状态,同时实现用户界面和交互操作,以及处理游戏的各种逻辑和事件。这样才能实现一个完整、流畅的俄罗斯方块游戏。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)