干货满满!!!面试必备OJ题:链表篇(一)

1、反转一个单链表

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
class Solution {
    public ListNode reverseList(ListNode head) {
       ListNode newHead = null;
       ListNode cur = head;
       ListNode prev = null;
        while(cur != null) {
            ListNode curNext = cur.next;
            if(curNext == null) {
                newHead = cur;
            }
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }
        return newHead;
    }
}

OJ链接

2、链表的中间节点

给定一个头结点为 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
示例1:

输入:[1,2,3,4,5]
输出:3

示例2:

输入:[1,2,3,4,5,6]
输出:4 (由于该列表有两个中间结点,值分别为 34,我们返回第二个结点。)
class Solution {
    public ListNode middleNode(ListNode head) {
       ListNode fast = head;
       ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}

OJ链接

3、回文链表

示例 1:
输入: 1 2
输出: false 

示例 2:
输入: 1 2 2 1
输出: true 
class Solution {
    public boolean isPalindrome(ListNode head) {
       if(head == null) {
            return true;
        }
        if(head.next == null) {
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //slow就是中间位置,开始进行第二步
        //2、进行反转
        ListNode cur = slow.next;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //3、开始判断
        while(head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            //偶数情况下
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }
}

OJ链接

4、判断链表是否有环

给定一个链表,判断链表中是否有环。
如果链表中存在环,则返回 true 。 否则,返回 false。
在这里插入图片描述

public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
               break;
            }
        }
        if(fast == null || fast.next == null) {
            return false;
        }
        return true; 
    }
}

OJ链接

5、环形链表

给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
         while(fast != null && fast.next != null) {
             fast = fast.next.next;
             slow = slow.next;
             if(fast == slow) {
                 break;
             }
         }
         if(fast == null || fast.next == null) {
             return null;
         }
             slow = head;
             while(slow != fast) {
             fast = fast.next;
             slow = slow.next;
         }
         return slow;
    }
}

OJ链接

6、删除重复节点

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。

public ListNode deleteDuplicates(ListNode head) {
      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;
    }

7、分隔链表

给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。

示例:
输入: head = 1->4->3->2->5->2, x = 3
输出: 1->2->2->4->3->5
//暴力解法
class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = head;
        while(cur != null) {
            if(cur.val < x) {
                if(bs == null) {
                    bs = cur;
                    be = cur;
                }else {
                    be.next = cur;
                    be = be.next;
                }
            }else {
                if(as == null) {
                    as = cur;
                    ae = cur;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs == null) {
            return as;
        }
        //bs!=null;
        be.next = as;
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }
}

OJ链接

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第一章 绪论作业答案(共50分) 一、分析如下程序中 (1)~ (10)各语句的频度。(每个1分,共10分) Ex( ) { int i , j , t ; (1) for( i=1 ; i<10 ; i++) //n = (2) printf(“\n %d” , i ); //n = (3) for(i=1; i<=2; i++) //n = (4) printf(“\n”); //n = (5) for(i=1; i<=9; i++) //n = { (6) for(j=1; j <= i ; j++) //n = { (7) t = i * j ; //n = (8) printf(“]”,t); //n = } (9) for(j=1; j 0) { if(x > 100) {x -= 10 ; y -- ;} else x ++ ; } 问if 语句执行了多少次?(2分) y--执行了多少次?(2分) x ++执行了多少次?(2分) 三、回答问(共25分) 书中16页的起泡排序如下: void bubble_sort(int a[],int n){ //将a中整数序列重新排列成自小至大有序的整数序列。 for(i=n-1,change=TRUE;i>=1&&change;--i){ change=FALSE; for(j=0;ja[j+1]{a[j]<-->a[j+1];change=TRUE; } } }//bubble_sort 1.(共15分)分析该算法的最佳情况 ,最坏情况和平均情况下各自的时间复杂度(给出分析思路与过程)。 (1) 最佳情况的时间复杂度分析(5分): (2) 最坏情况的时间复杂度分析(5分): (3) 平均情况的时间复杂度分析(5分): 2.(共10分)比较与C语言书中的起泡排序异同,并从时空效率角度说明谁更优。 四、完成如下选择(每3分,共9分)。 1. 设f为原操作,则如下算法的时间复杂度是( )。 for (i = 1; i*i=1;i--) for(j=1;jA[j+1]) A[j]与A[j+1]对换; 其中n为正整数,则算法在最坏情况下的时间复杂度为( )。 A.O(n) B.O(nlog2n) C. O(n3) D. O(n2)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值