Chain Surfase Test - java 链表经典 OJ 面试题 - 巨细
创始人
2024-03-21 13:51:06
0

在这里插入图片描述


效果图

在这里插入图片描述


LeetCode - 206. 反转链表

===================================================================================

代码如下:


/**

  • 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 ListNode reverseList(ListNode head) {

if(head==null){

return null;

}

ListNode prev = null;

ListNode cur = head;

while(cur!=null){

ListNode curNext = cur.next;

cur.next = prev;

prev = cur;

cur = curNext;

}

return prev;

}

}

图解

在这里插入图片描述


效果图

在这里插入图片描述


LeetCpde - 876. 链表的中间结点

======================================================================================

代码如下:


/**

  • 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 ListNode middleNode(ListNode head) {

if(head==null){

return null;

}

ListNode fast = head;

ListNode slow = head;

while(fast!=null&&fast.next!=null){

fast = fast.next.next;

slow = slow.next;

}

return slow;

}

}

图解

在这里插入图片描述


效果图

在这里插入图片描述


题霸 - 链表中倒数第k个结点

==============================================================================

代码如下:


/*

public class ListNode {

int val;

ListNode next = null;

ListNode(int val) {

this.val = val;

}

}*/

public class Solution {

public ListNode FindKthToTail(ListNode head,int k) {

if(head==null|| k<=0){

return null;

}

ListNode fast = head;

ListNode slow = head;

while(k-1!=0){

fast=fast.next;

if(fast==null){

return null;

}

k–;

}

while(fast.next!=null){

fast=fast.next;

slow = slow.next;

}

return slow;

}

}

附图

在这里插入图片描述

效果图

在这里插入图片描述


LeetCode - 21 - 合并两个有序链表

=======================================================================================

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

代码如下


/**

  • 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 ListNode mergeTwoLists(ListNode l1, ListNode l2) {

ListNode head1= l1;

ListNode head2 = l2;

ListNode newHead = new ListNode();

ListNode p = newHead;

while(head1!=null&&head2!=null){

if(head1.val

p.next = head1;

p = p.next;

head1=head1.next;

}else{// head1,val >= head2。val

p.next = head2;

p = p.next;

head2 = head2.next;

}

}

if(head1==null){

p.next = head2;

}

if(head2==null){

p.next = head1;

}

return newHead.next;

}

}

图解

在这里插入图片描述


效果图

在这里插入图片描述


题霸 - 链表分割

========================================================================

现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,

且不能改变原来的数据顺序,返回重新排列后的链表的头指针。

代码如下:


import java.util.*;

/*

public class ListNode {

int val;

ListNode next = null;

ListNode(int val) {

this.val = val;

}

}*/

public class Partition {

public ListNode partition(ListNode pHead, int x) {

ListNode a = null;

ListNode b = a;// [a,b] 存储小于x的值

ListNode c = null;

ListNode d = c;// [c.d] 存储大于或等于的x的值

ListNode cur = pHead;

while(cur!=null){

if(cur.val

if(a==null){

a = cur;

b = cur;

}else{

b.next = cur;

b = b.next;

}

}else{

if(c==null){

c=cur;

d=cur;

}else{

d.next = cur;

d = d.next;

}

}

cur = cur.next;

}

if(a == null){

return c;

}

if(c==null){

return a;

}

if(d,next !=null){

d.next = null;

}

b.next = c;

return a;

}

}

图解

在这里插入图片描述

效果图

在这里插入图片描述


题霸 - 删除链表(有序的)中重复的结点

===================================================================================

代码如下:


/*

public class ListNode {

int val;

ListNode next = null;

ListNode(int val) {

this.val = val;

}

}

*/

public class Solution {

public ListNode deleteDuplication(ListNode head) {

if(head==null){

return null;

}

ListNode cur = head;

ListNode newHead = new ListNode(-1);

ListNode tmp = newHead;

while(cur!=null){

if(cur.next!=null&&cur.val==cur.next.val){

while(cur.next!=null&&cur.val==cur.next.val){

cur=cur.next;

}

cur =cur.next;

}else{

tmp.next = cur;

tmp = tmp.next;

cur = cur.next;

}

}

tmp.next = null;

return newHead.next;

}

}

图解

在这里插入图片描述

效果图

![在这里插入图片描述](h

ttps://img-blog.csdnimg.cn/e116cfed3c88487b8c5acb2d9c0dcc6b.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBARGFyayBBbmQgR3JleQ==,size_20,color_FFFFFF,t_70,g_se,x_16)


题霸 - 链表的回文结构

===========================================================================

对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。

给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。

测试样例:(正反都一样)

1 2 3 2 1

代码如下:


import java.util.*;

/*

public class ListNode {

int val;

ListNode next = null;

ListNode(int val) {

this.val = val;

}

}*/

public class PalindromeList {

public boolean chkPalindrome(ListNode head) {

// write code here

if(head==null){

return true;

}

ListNode fast = head;

ListNode slow = head;

while(fast!=null&&fast.next!=null){

fast = fast.next.next;

slow = slow.next;

}

ListNode cur = slow.next;

while(cur!=null){

ListNode curNext = cur.next;

cur.next = slow;

slow = cur;

cur = curNext;

}

while(head!=slow){

if(head.val!=slow.val){

return false;

}

if(head.next == slow){

return true;

}

head = head.next;

slow = slow.next;

}

return true;

}

}

图解

在这里插入图片描述

效果图

在这里插入图片描述


LeetCode - 160. 相交链表

===================================================================================

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

题目数据 保证 整个链式结构中不存在环。

注意,函数返回结果后,链表必须 保持其原始结构 。

代码如下


/**

  • Definition for singly-linked list.

  • public class ListNode {

  • int val;
    
  • ListNode next;
    
  • ListNode(int x) {
    
  •     val = x;
    
  •     next = null;
    
  • }
    
  • }

*/

public class Solution {

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

if(headA==null||headB == null){

return null;

}

ListNode pl = headA;

ListNode ps = headB;

int lenA = 0;

int lenB = 0;

while(pl!=null){

lenA++;

pl = pl.next;

}

pl = headA;

while(ps!=null){

lenB++;

ps = ps.next;

}

ps = headB;

int len = lenA-lenB;

if( len < 0){

pl = headB;

ps = headA;

len = lenB -lenA;

}

while(len!=0){

pl = pl.next;

len–;

}

while(pl!=ps){

pl = pl.next;

ps = ps.next;

}

return pl;

}

}

附图


在这里插入图片描述


附图

在这里插入图片描述


力扣神奇解法

在这里插入图片描述


Leet Code - 141 - 环形链表

=====================================================================================

给定一个链表,判断链表中是否有环。

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,

我们使用整数 pos 来表示链表尾连接到链表中的位 置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。

如果链表中存在环,则返回 true 。 否则,返回 false 。

代码如下:


/**

  • Definition for singly-linked list.

  • class ListNode {

  • int val;
    
  • ListNode next;
    
  • ListNode(int x) {
    
  •     val = x;
    
  •     next = null;
    
  • }
    
  • }

*/

public class Solution {

public boolean hasCycle(ListNode head) {

if(head == null){

return false;

}

ListNode fast = head;

ListNode slow = head;

while(fast!=null&&fast.next!=null){

fast = fast.next.next;

if(fast==slow){

return true;

}

slow = slow.next;

}

return false;

}

}

附图

在这里插入图片描述


效果图

在这里插入图片描述


LeetCode - 141 - 环形链表 ||

=======================================================================================

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。

如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用 于标识环的情况,并不会作为参数传递到函数中。

说明:不允许修改给定的链表。

进阶:

你是否可以使用 O(1) 空间解决此题?

代码如下


(head == null){

return false;

}

ListNode fast = head;

ListNode slow = head;

while(fast!=null&&fast.next!=null){

fast = fast.next.next;

if(fast==slow){

return true;

}

slow = slow.next;

}

return false;

}

}

附图

在这里插入图片描述


效果图

在这里插入图片描述


LeetCode - 141 - 环形链表 ||

=======================================================================================

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。

如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用 于标识环的情况,并不会作为参数传递到函数中。

说明:不允许修改给定的链表。

进阶:

你是否可以使用 O(1) 空间解决此题?

代码如下


相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...