java单链表的逆置
时间: 2023-11-04 21:01:09 浏览: 78
在Java中,要实现单链表的逆置,可以根据引用中提供的代码进行操作。代码的思路是通过遍历链表,将每个节点进行头插法插入到一个新的链表中,最后返回新链表的头节点。具体步骤如下:
1. 首先判断链表是否为空,如果为空则直接返回null。
2. 创建两个指针last和cur,分别指向原链表的头节点和头节点的下一个节点。
3. 创建一个临时节点temp,用于保存当前节点cur。
4. 在循环中,将cur节点的next指向last节点,然后更新last节点为cur节点,cur节点为temp节点的下一个节点。
5. 循环直到cur节点为空,即完成了链表的逆置。
6. 最后将last节点的next指向null,返回last节点作为逆置后链表的头节点。
这样就实现了Java单链表的逆置。
相关问题
java单链表就地逆置
以下是Java实现单链表就地逆置的代码:
```
public class ListNode {
int val;
ListNode next;
public ListNode(int val) {
this.val = val;
this.next = null;
}
}
public class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
return prev;
}
}
```
主要思路是从头开始遍历链表,每次将当前节点的next指向前面的节点,然后将prev指向当前节点,当前节点指向下一个节点,继续遍历下去,直到遍历完整个链表。最后返回prev即为逆置后的链表头。
单链表逆置c语言代码
单链表逆置的C语言代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
struct ListNode {
int val;
struct ListNode* next;
};
// 单链表逆置函数
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode* prev = NULL;
struct ListNode* curr = head;
while (curr != NULL) {
struct ListNode* nextTmp = curr->next;
curr->next = prev;
prev = curr;
curr = nextTmp;
}
return prev;
}
// 测试函数
void test() {
// 创建链表节点
struct ListNode* node1 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* node2 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* node3 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* node4 = (struct ListNode*)malloc(sizeof(struct ListNode));
// 初始化链表节点的值
node1->val = 1;
node2->val = 2;
node3->val = 3;
node4->val = 4;
// 构建链表关系
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = NULL;
// 输出原始链表
printf("原始链表:");
struct ListNode* curr = node1;
while (curr != NULL) {
printf("%d ", curr->val);
curr = curr->next;
}
// 单链表逆置
struct ListNode* newHead = reverseList(node1);
// 输出逆置后的链表
printf("\n逆置后的链表:");
curr = newHead;
while (curr != NULL) {
printf("%d ", curr->val);
curr = curr->next;
}
// 释放内存
free(node1);
free(node2);
free(node3);
free(node4);
}
int main() {
test();
return 0;
}
```
相关推荐
![](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)