将一个有序链表倒置(C语言实现)
时间: 2023-05-28 19:01:50 浏览: 51
//定义链表节点结构体
typedef struct ListNode {
int val;
struct ListNode *next;
} ListNode;
//将有序链表倒置函数
struct ListNode* reverseList(struct ListNode* head){
struct ListNode *new_head = NULL; //新建一个指针,指向最后一个节点,初始为空
while (head != NULL) { //如果原链表不为空
struct ListNode *temp = head->next; //将原链表的下一个节点暂存
head->next = new_head; //将原链表的下一个节点指向新链表头部
new_head = head; //将新链表头部指向当前节点
head = temp; //将原链表头部指向下一个节点
}
return new_head; //返回新链表头部
}
相关问题
用C语言实现一个有序链表
以下是一个基于C语言实现的有序链表示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createNode(int data) {
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
void insertNode(Node** head_ref, Node* new_node) {
Node* current;
if (*head_ref == NULL || (*head_ref)->data >= new_node->data) {
new_node->next = *head_ref;
*head_ref = new_node;
} else {
current = *head_ref;
while (current->next != NULL && current->next->data < new_node->data) {
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
void printList(Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
Node* node1 = createNode(10);
Node* node2 = createNode(15);
Node* node3 = createNode(5);
Node* node4 = createNode(20);
insertNode(&head, node1);
insertNode(&head, node2);
insertNode(&head, node3);
insertNode(&head, node4);
printList(head);
return 0;
}
```
该程序创建了一个`Node`结构体,该结构体有两个成员,一个是整数型数据`data`,另一个是一个指向下一个节点的指针。
`createNode`函数负责创建一个新节点并返回一个指向该节点的指针。
`insertNode`函数则负责将一个新节点插入到有序链表中。插入过程会根据节点数据的大小调整节点之间的链接。
`printList`函数用于打印整个链表。
在`main`函数中,我们创建了四个节点,并将它们插入到有序链表中。最后,我们打印整个链表以检查它是否按照我们想要的顺序排列。
两个有序链表的交集c语言实现
以下是两个有序链表的交集的C语言实现代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
struct ListNode {
int val;
struct ListNode *next;
};
// 定义函数,用于创建链表节点
struct ListNode* createListNode(int val) {
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->val = val;
node->next = NULL;
return node;
}
// 定义函数,用于打印链表
void printList(struct ListNode* head) {
struct ListNode* p = head;
while(p != NULL) {
printf("%d ", p->val);
p = p->next;
}
printf("\n");
}
// 定义函数,用于获取两个有序链表的交集
struct ListNode* getIntersection(struct ListNode* head1, struct ListNode* head2) {
struct ListNode* p1 = head1;
struct ListNode* p2 = head2;
struct ListNode* head = NULL;
struct ListNode* tail = NULL;
while(p1 != NULL && p2 != NULL) {
if(p1->val == p2->val) {
struct ListNode* node = createListNode(p1->val);
if(head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
p1 = p1->next;
p2 = p2->next;
} else if(p1->val < p2->val) {
p1 = p1->next;
} else {
p2 = p2->next;
}
}
return head;
}
int main() {
// 创建链表1: 1->2->4->5
struct ListNode* head1 = createListNode(1);
head1->next = createListNode(2);
head1->next->next = createListNode(4);
head1->next->next->next = createListNode(5);
printf("链表1: ");
printList(head1);
// 创建链表2: 2->3->5->6
struct ListNode* head2 = createListNode(2);
head2->next = createListNode(3);
head2->next->next = createListNode(5);
head2->next->next->next = createListNode(6);
printf("链表2: ");
printList(head2);
// 获取两个有序链表的交集
struct ListNode* intersection = getIntersection(head1, head2);
printf("交集: ");
printList(intersection);
return 0;
}
```
该代码首先定义了一个链表节点结构体,然后定义了三个函数:`createListNode()`用于创建链表节点,`printList()`用于打印链表,`getIntersection()`用于获取两个有序链表的交集。
在`getIntersection()`函数中,定义了两个指针`p1`和`p2`,分别指向两个链表的头结点。然后使用while循环遍历这两个链表,如果`p1->val`等于`p2->val`,就说明这个节点是交集中的元素,于是创建一个新的节点,并将其添加到交集链表中。如果`p1->val`小于`p2->val`,说明`p1`所指向的节点在`p2`所指向的节点之前,因此将`p1`向后移动一位。如果`p1->val`大于`p2->val`,说明`p2`所指向的节点在`p1`所指向的节点之前,因此将`p2`向后移动一位。最后返回交集链表的头结点。
在`main()`函数中,先创建了两个有序链表,然后调用`getIntersection()`函数获取它们的交集,并打印输出。