题目链接
#include<iostream>
using namespace std;
// 定义链表节点结构体
struct LinkedNode {
int val;
LinkedNode* next;
LinkedNode(int val):val(val), next(nullptr){}
};
LinkedNode* reverseList(LinkedNode* head) {
LinkedNode* temp; // 保存cur的下一个节点
LinkedNode* cur = head;
LinkedNode* pre = NULL;
while(cur) {
temp = cur->next; // 保存一下 cur的下一个节点,因为接下来要改变cur->next
cur->next = pre; // 翻转操作
// 更新pre 和 cur指针
pre = cur;
cur = temp;
}
return pre;
}
void printLinkedList(LinkedNode* head) {
LinkedNode* cur = head;
while (cur != nullptr) {
cout << cur->val << " ";
cur = cur->next;
}
cout << endl;
}
int main() {
int n, m;
LinkedNode* dummyHead = new LinkedNode(0); // 这里定义的头结点 是一个虚拟头结点,而不是真正的链表头结点
while (cin >> n) {
if (n == 0) {
cout << "list is empty" << endl;
continue;
}
LinkedNode* cur = dummyHead;
while (n--) {
cin >> m;
LinkedNode* newNode = new LinkedNode(m); // 开始构造节点
cur->next = newNode;
cur = cur->next;
}
printLinkedList(dummyHead->next);
printLinkedList(reverseList(dummyHead->next));
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String[] str = sc.nextLine().split(" ");
if (Integer.parseInt(str[0]) == 0) {
System.out.println("list is empty");
}
ListNode dummyhead = new ListNode(-1);
ListNode cur = dummyhead;
//构造链表
for (int i = 1; i < str.length; i++) {
ListNode temp = new ListNode(Integer.parseInt(str[i]));
cur.next = temp;
cur = cur.next;
if (i == str.length - 1) cur.next = null;
}
//输出原函数
ListNode pointer = dummyhead.next;
while (pointer != null) {
System.out.print(pointer.val + " ");
pointer = pointer.next;
}
System.out.println();
//反转链表
ListNode prev = null;
ListNode curr = dummyhead.next;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
//输出反转链表
ListNode pointer2 = prev;
while (pointer2 != null) {
System.out.print(pointer2.val + " ");
pointer2 = pointer2.next;
}
System.out.println();
}
}
}
class ListNode{
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
#定义链表节点
class LinkedNode:
def __init__(self, val = 0, next = None):
self.val = val
self.next = next
def reverseList(head: LinkedNode) -> LinkedNode:
temp = None # 保存cur的下一个节点
cur = head
pre = None
while cur:
temp = cur.next
cur.next = pre # 翻转操作
pre = cur # 更新pre 和 cur指针
cur = temp
return pre
def printLinkedList(head: LinkedNode):
cur = head
while cur:
print(cur.val, end = " ")
cur = cur.next
print()
if __name__ == "__main__":
while True:
try:
# 输入5 1 2 3 4 5,表示链表有5个节点,值分别为1 2 3 4 5
n, *nums = map(int, input().split())
except:
break
if n == 0:
print("list is empty")
continue
dummyHead = LinkedNode(0) # 这里定义的头结点 是一个虚拟头结点,而不是真正的链表头结点
cur = dummyHead
for i in range(n): # 开始构造节点
cur.next = LinkedNode(nums[i])
cur = cur.next
printLinkedList(dummyHead.next) # 打印链表
printLinkedList(reverseList(dummyHead.next)) # 打印翻转后的链表
// 引入readline模块来读取标准输入
const readline = require('readline')
// 创建readline接口
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
// 处理输入和输出
rl.on('line', (input) => {
// 将每一行以空格分割成一个字符串数组,并将每个元素转换成number类型
const line = input.split(' ').filter(item => item !== '').map(Number)
// 第一个元素是链表长度
const n = line[0]
// 长度为0,直接输出 list is empty
if (n === 0) {
console.log('list is empty')
return
}
// 根据给定输入创建链表
let head = createLinkedList(line.slice(1))
// 打印翻转前的链表
printLinkedList(head)
// 翻转链表
head = reverseLinkedList(head)
// 打印翻转后的链表
printLinkedList(head)
})
// 链表节点定义
class Node {
constructor(val, next = null) {
this.val = val
this.next = next
}
}
// 给定一个number数组,创建出链表,返回链表的头节点
function createLinkedList(arr) {
// 创建头节点
const head = new Node(arr[0])
// 初始化尾指针,方便添加新的节点
let tail = head
arr.slice(1).forEach(item => {
// 每次将细节点插在尾节点后面
tail.next = new Node(item)
// 更新尾节点为新创建的节点
tail = tail.next
})
// 返回头节点
return head
}
// 翻转链表
function reverseLinkedList(head) {
// 前一个节点初始化为null
let prev = null
// 当前节点初始化为head
let curr = head
while(curr) {
// 临时存储当前节点
const temp = curr
// 指向下一个节点
curr = curr.next
// 将当前节点的next指向前一个节点
temp.next = prev
// 前一个节点指向当前节点,相当于往后移动一个节点
prev = temp
}
return prev
}
// 输出链表
function printLinkedList(head) {
let output = ''
// 将每个节点的val拼接成一个字符串
while(head) {
output += `${head.val} `
head = head.next
}
// 最后输出
console.log(output)
}
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
struct LinkedNode {
int val;
struct LinkedNode* next;
};
struct LinkedNode* reverseList(struct LinkedNode* head) {
struct LinkedNode* temp; // 保存 cur 的下一个节点
struct LinkedNode* cur = head;
struct LinkedNode* pre = NULL;
while (cur) {
temp = cur->next; // 保存一下 cur 的下一个节点,因为接下来要改变 cur->next
cur->next = pre; // 翻转操作
// 更新 pre 和 cur 指针
pre = cur;
cur = temp;
}
return pre;
}
void printLinkedList(struct LinkedNode* head) {
struct LinkedNode* cur = head;
while (cur != NULL) {
printf("%d ", cur->val);
cur = cur->next;
}
printf("\n");
}
int main() {
int n, m;
struct LinkedNode* dummyHead = (struct LinkedNode*)malloc(sizeof(struct LinkedNode)); // 这里定义的头结点是一个虚拟头结点,而不是真正的链表头结点
dummyHead->next = NULL;
struct LinkedNode* cur = dummyHead;
while (scanf("%d", &n) == 1) {
if (n == 0) {
printf("list is empty\n");
continue;
}
while (n--) {
scanf("%d", &m);
struct LinkedNode* newNode = (struct LinkedNode*)malloc(sizeof(struct LinkedNode)); // 开始构造节点
newNode->val = m;
newNode->next = NULL;
cur->next = newNode;
cur = cur->next;
}
printLinkedList(dummyHead->next);
printLinkedList(reverseList(dummyHead->next));
}
return 0;
}