leetcode链表相关经典习题:

141.给定一个链表,判断链表中是否有环。
public class Solution {
public boolean hasCycle(ListNode head) {
if(headnull||head.nextnull){ //若链表为空,或链表中只有一个元素,则一定无环;
return false;
}
ListNode fast=head;
ListNode slow=head;
while(fast!=null&& fast.next!=null){ //当fast和fast.next 不为空,slow向后移动一步,fast向后移动两步
slow=slow.next;
fast=fast.next.next;
if(slow==fast){ //当fast和slow重合时候,成环;
return true;
}
}
return false;
}
}
142.给定一个链表,返回链表中第一个入环的节点,若没有环,返回null;

public class Solution {
public ListNode detectCycle(ListNode head) {
if(headnull||head.nextnull){
return null;
}
ListNode fast=head;
ListNode slow=head;
while(fast!=null&&fast.next!=null){ //如果链表中存在元素,fast和slow分别后移
fast=fast.next.next;
slow=slow.next;
if(fastslow){ //如果重合,则成环跳出
break;
}
}
if(fast
null||fast.nextnull){ //如果链表为,返回null
return null;
}
while(fast!=head){
fast=fast.next;
head=head.next;
}
return fast;
}
}
16.给定两个链表,判断两个链表是否有交点。
思路:设置两个指针p1和p2.
p1从 headA 开始,p2从 headB 开始;
p1 p2分别从表头开始每次向后移动一个节点,移动到末尾处时,再换一个链表从头向后移动,如果p1=p2,则证明有交点,否则返回null;
代码如下:
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA
null||headBnull){
return null;
}
ListNode p1=headA;
ListNode p2=headB;
boolean isp1Change=false;
boolean isp2Change=false;
while(p1!=null||p2!=null){
if(p1
p2){
return p1;//或者p2
}

        p1=p1.next;
        p2=p2.next;
        if(p1.next==null){//第一遍p1走到最后一个位置后,换到另一个表头继续遍历;
            p1=headB;
           isp1Change=true;
        }
         if(p2.next==null){
            p2=headA;
            isp2Change=true;
        }
        if(isp1Change&&isp2Change&&p1==null&&p2==null){
            return  null;
        }
    }
    return  null;
}

}

19,删除链表的倒数第n个节点
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(headnull||head.nextnull){
return null;
}
ListNode p1=head;
ListNode p2=head;
for(int i=0;i<n;i++){//删除第n个元素,p1先向后移动n位
p1=p1.next;
}
if(p1==null){//p1走完后为null,则代表删除的是第一个元素
return head.next;
}
while(p1!=null){
p1=p1.next;
p2=p2.next;
}
p2.next=p2.next.next;
return head;

}

}
206.反转链表:如123456–>654321(迭代方法)
class Solution {
public ListNode reverseList(ListNode head) {
if(headnull||head.nextnull){
return head;
}
ListNode root=new ListNode(0);
ListNode n=null;
while(head!=null){
n=head;
head=head.next;
n.next=null;
n.next=root.next;
root.next=n;
}
return root.next;
}
}
206反转链表:如123456–>654321(递归方法)
class Solution {
public ListNode reverseList(ListNode head) {
if(headnull||head.nextnull){
return head;
}
ListNode h=reverseList(head.next);
head.next.next=head;
head.next=null;
return h;
}
}
21.合并两个有序链表为一个新链表;
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode p1=l1;
ListNode p2=l2;
ListNode p3=new ListNode(0);
ListNode p=p3;
while(true){
if(p1null&&p2null){
break;
}else if(p1!=null&&p2null){
p.next=p1;
p1=p1.next;
}else if(p1
null&&p2!=null){
p.next=p2;
p2=p2.next;
}else if(p1.val<=p2.val){
p.next=p1;
p1=p1.next;
}else{
p.next=p2;
p2=p2.next;
}
p=p.next;
}
return p3.next;
}
}

724给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法。
我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和。
如果数组不存在中心索引,那么我们应该返回 -1。如果数组有多个中心索引,那么我们应该返回最靠近左边的那一个

class Solution {
public int pivotIndex(int[] nums) {
if(nums.length<2){
return -1;
}
int sum=0;
int left=0;
int right=0;
for(int num:nums){
sum=sum+num;
}

    for(int i=0;i<nums.length;i++){
        if(i==0){
           left=0; 
        }else{
            left+=nums[i-1];
        }
        right=sum-left-nums[i];
        if(right==left){
         return i;   
        }
    }
    return -1;
}

}

2.两数相加:给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode root=new ListNode(0);
ListNode p=root;
int carry=0;
while(l1!=null||l2!=null||carry!=0){
int num=(l1null?0:l1.val)+(l2null?0:l2.val)+carry;
ListNode n=new ListNode(num%10);
n.next=p.next;
p.next=n;
p=n;
carry=num/10;

        l1=(l1==null)?null:l1.next;
        l2=(l2==null)?null:l2.next;
    }
    return root.next;
}

}
169.求数组中的众数;
class Solution {
public int majorityElement(int[] nums) {
int max=nums[0];
int count=1;
for(int i=0;i<nums.length;i++){
if(nums[i]max){
count++;
}else{
count–;
if(count
0){
max=nums[i];
count=1;
}
}
}
return max;
}
}
240. 搜索二维矩阵 II:编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性:
每行的元素从左到右升序排列。
每列的元素从上到下升序排列。
示例:
现有矩阵 matrix 如下:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
给定 target = 5,返回 true。
给定 target = 20,返回 false。

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix.length==0){
return false;
}
int row=matrix.length;
int col=matrix[0].length;
int x=row-1;
int y=0;
while(x>=0&&y<col){
if(matrix[x][y]==target){
return true;
}else if(matrix[x][y]<target){
y++;
}else{
x–;
}
}
return false;
}
}

猜你喜欢

转载自blog.csdn.net/zhangpupu320/article/details/88920993
今日推荐