文章目录
前言
上一篇博客我们学习到了单链表的相关操作,今天我们来利用几道题进行汇总。
1.移除链表元素
2.反转链表
3.链表的中间结点
4.返回倒数第k个节点
5.合并两个有序链表
6.链表分割
7.链表的回文结构
8.相交链表
9.环形链表 一
10.环形链表 二
足足十道题,咱们慢慢干!
1.移除链表元素
提高难度:只遍历一次。
这道题咱们在上一篇博客中已经模拟实现过了, 咱们在写一次。
因为他是个单链表,在遍历的过程中,只可以得到一个节点(cur)以及其该节点的下一个节点,当cur.val == val 就需要知道cur前一个节点,那么就需要两个引用(Java中没有指针这一说,用引用来代替),来遍历这个链表,一个是cur,另外一个是pcur。
然后让cur一直往前走,pcur在cur的后面,分为两种情况,第一种情况(cur.val != val),那么就pcur 到 cur 的位置上(pcur = cur),cur往后移动(cur = cur.next),第二种情况(cur.val == val),prev的下一个节点(prev.next)指向到(cur.next),同时cur移动到cur.next(cur = cur.next)中,直到遍历完整个链表,
注意:如果头结点也是要删除的对象的话,那么,直接将头节点移动到下一个节点即可(head = head.next)
还有如果头结点是null的话,那么直接返回null即可。
根据上述的思路,我们写一下代码
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head == null){
return null;
}
ListNode pcur = head;
ListNode cur = head.next;
while(cur != null){
if(cur.val != val){
pcur = cur;
cur = cur.next;
}else{
pcur.next = cur.next;
cur = cur.next;
}
}
if (head.val == val){
head = head.next;
}
return head;
}
}
成功!
2.反转链表
这道题使用迭代或者递归都可以。我们讲迭代的思路。
其实这道题的将原来的头变成尾,将原来的尾变成头,怎么发生变化呢?答案是进行尾插法。那么就需要一个节点去遍历整个链表,在把头结点利用尾插法,最后形成的链表就是一个反转链表。
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null){
return head;
}
ListNode cur = head.next;
head.next = null;
while (cur != null){
ListNode curN = cur.next;
cur.next = head;
head = cur;
cur = curN;
}
return head;
}
}
成功
3.链表的中间结点
这道题不难,有多种方法,最简单就是先遍历一遍,知道链表的长度,然后,重新遍历,找到中间的节点,
提高要求:只遍历一次!
还有一种解题思路,这道题的属于一种类型题,叫做快慢指针问题,在这的意思是有两个节点(fast 和 slow),fast跑的快,slow跑的慢。
在这道题中,因为要找到中间节点即可,那么只需要fast每次走两步,slow每次走一步,那么slow节点就是要寻找的中间节点。
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;
}
}
4.返回倒数第k个节点
这道题的思路跟上一道题一样。还是快慢指针的问题。定义2个指针(fast 和 slow),要求返回倒数第k个节点,意思就是说,当fast为空的时候,slow和fast中还差个k个节点,那么就先让fast多走k步,然后两个节点齐头并进,最后返回slow节点。
class Solution {
public int kthToLast(ListNode head, int k) {
ListNode fast = head;
ListNode slow = fast;
while(k-1>=0){
fast = fast.next;
k--;
}
while(fast!=null){
fast = fast.next;
slow = slow.next;
}
return slow.val;
}
}
5.合并两个有序链表
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode head = new ListNode(-1);
ListNode cur = head;
while(list1!=null && list2!=null){
if(list1.val < list2.val){
cur.next = list1;
list1 = list1.next;
cur = cur.next;
}else{
cur.next = list2;
list2 = list2.next;
cur = cur.next;
}
}
if (list1!=null){
cur.next = list1;
}
if (list2!=null){
cur.next = list2;
}
return head.next;
}
}
6.链表分割
解题思路:
public class Partition {
public ListNode partition(ListNode pHead, int x) {
ListNode bs = null;
ListNode be = null;
ListNode as = null;
ListNode ae = null;
ListNode cur = pHead;
while (cur != null){
if (cur.val < x){
if (bs == null && be == null){
bs = be = cur;
}else{
be.next =cur;
be = be.next;
}
}else{
if (as == null && ae == null){
as = ae = cur;
}else{
ae.next =cur;
ae = ae.next;
}
}
cur = cur.next;
}
//万一没有be,意思就是没有比小的节点组成的链表
if (be == null){
return as;
}
be.next = as;
// 可能到最后 ae 的尾节点不为null
if (ae != null ){
ae.next = null;
}
return bs;
}
}
7.链表的回文结构
这道题是上面几道题的组合:
1.链表的中间结点
2.反转链表
public class PalindromeList {
public boolean chkPalindrome(ListNode A) {
if (A == null){
return false;
}
//1.找中间节点
ListNode fast = A;
ListNode slow = A;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}
// 2.进行尾插
ListNode cur = slow.next;
while(cur != null){
ListNode curN = cur.next;
cur.next = slow;
slow = cur;
cur = curN;
}
// 3.判断回文
while(A != slow ){
if (A.val != slow.val){
return false;
}
if (A.next == slow){
return true;
}
A = A.next;
slow = slow.next;
}
return true;
}
}
8.相交链表
这道题一看,跟上面的倒数第k个节点的题目如出一辙。
那就是把A链表和B链表分别遍历一遍,分别求出节点个数,然后判断出节点多的链表,然后从头开始,节点多的先走(节点个数的差值)步,其次在一起走,相遇俄点就是第一个相交的点
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode pl = headA;
ListNode ps = headB;
// 1.分别计算节点数目
int lenA = 0;
int lenB = 0;
while(pl != null){
lenA++;
pl = pl.next;
}
while(ps != null){
lenB++;
ps = ps.next;
}
pl = headA;
ps = headB;
// 2.判断大小
int len = lenA - lenB;
if (len < 0){
pl = headB;
ps = headA;
len = lenB - lenA;
}
// 3. pl 先走 len 步,然后pl和ps同时遍历
while(len>0){
pl = pl.next;
len--;
}
while(ps != pl){
ps = ps.next;
pl = pl.next;
}
if (pl == null){
return null;
}
return pl;
}
}
9.环形链表 一
这道题又是一道快慢指针的问题,主要是要想 快比慢多走几步。
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){
return true;
}
}
return false;
}
}
10.环形链表 二
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){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
下一篇,我们要学习双向链表的模拟实现,大家把上面的这些题消化完,就开始下一篇的学习吧。