Chain Surfase Test - java 链表经典 OJ 面试题 - 巨细

x33g5p2x  于2021-11-22 转载在 Java  
字(6.1k)|赞(0)|评价(0)|浏览(198)

LeetCode - 203. 移除链表元素在链表博客中讲过

给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。

这里就不将了了。

代码

/** * 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 removeElements(ListNode head, int val) {
         if(head==null){
             return null;// 判断头节点是否null
         }
         ListNode cur =  head.next;
         ListNode prev = head;
         while(cur!=null){
             if(cur.val == val){
                 prev.next=cur.next;
                 cur=cur.next;
             }else{
                 prev = cur;
                 cur = cur.next;
             }
         }
         
         if(head.val == val){ // 判断头节点是否是需要删除的元素
             head = head.next;
         }
         return head;
    }
}

附图讲解

效果图

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<head2.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<x){
                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;
    }
}

图解

效果图

题霸 - 链表的回文结构

对于一个链表,请设计一个时间复杂度为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) 空间解决此题?

代码如下

/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */
public class Solution {
    public ListNode detectCycle(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;
            if(fast == slow){
                ListNode tmp = head;
                while(tmp!=slow){
                    tmp = tmp.next;
                    slow = slow.next;
                }
                return tmp;
            }
        }
        return null;
    }
}

LeetCode 讲解

效果图

本文结束

相关文章