Java中链表的示例分析
作者:WeekendLife
时间:2023-05-01
浏览:0
题目一解法/***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode(){}*ListNode(intval){this.val=val;}*ListNode(intval,ListNodenext){this.val=val;this.next=next;}*}*/classSolution{publicintgetDecimalValue(ListNodehead){int[]arr=n
本文目录
题目一

解法
/**
* 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 int getDecimalValue(ListNode head) {
int[] arr = new int[31];
int index = 0;
int ans = 0;
while(head!=null){
arr[index] = head.val;
index++;
head = head.next;
}
for(int i = 0;i题目二

解法
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
int index = 0;
ListNode h = head;
while(head!=null){
head = head.next;
index++;
}
int[] arr = new int[index];
while(h!=null){
arr[index-1] = h.val;
index--;
h = h.next;
}
return arr;
}
}题目三

解法
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode node = new ListNode(-1);
ListNode ans = node;
while(l1!=null&&l2!=null){
if(l1.val<=l2.val){
node.next = l1;
l1 = l1.next;
}else{
node.next = l2;
l2 = l2.next;
}
node = node.next;
}
if(l1!=null){
node.next = l1;
}
if(l2!=null){
node.next = l2;
}
return ans.next;
}
}
作者最新文章
漫蛙漫画官方入口
2026-10-13 13:50
漫蛙官网入口免费下载最新版
2026-10-13 13:46
漫蛙网页版入口在哪里
2026-10-13 13:18
漫蛙网页版免费进入
2026-10-13 13:16
漫蛙MANWA网页版登录不迷路
2026-10-13 13:16
上一篇:
Java怎么对文件进行基本操作
下一篇:
Python怎么读取预处理DICOM文件
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多










































