【数据结构】链表练习题

移除链表元素

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        while(pre != null && pre.next != null){
             if(pre.next.val == val){
                 pre.next = pre.next.next;
             }else{
                 pre = pre.next;
             }      
        }
        return dummy.next;
    }
}

1. 链表的中间结点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
// 快慢指针:
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}
//普通方法:
class Solution {
    public ListNode middleNode(ListNode head) {
        int len = 0;
        ListNode cur = head;
        while(cur != null){
            cur = cur.next;
            len++;
        }
        int len2 = len/2;
        cur = head;
        for(int i = 0; i < len2; i++){
            cur = cur.next;
        }
        return cur;
    }
}

2. 链表中倒数第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) {
        ListNode slow = head;
        ListNode fast = head;
        int i = 0;
        while(fast != null){
            fast = fast.next;
            if(i >= k){
                slow = slow.next;
            }
            i++;
        }
        if(i < k){
            return null;
        }
            
        return slow;
    }
} 

3. 链表分割

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) {
        if(pHead == null && pHead.next == null){
            return pHead;
        }
        ListNode bStart = new ListNode(-1);
        ListNode bEnd = bStart;
        ListNode aStart = new ListNode(-1);
        ListNode aEnd = aStart;
        ListNode cur = pHead;
        while(cur != null){
            if(cur.val < x){
                bEnd.next =  new ListNode(cur.val);
                bEnd = bEnd.next;
            }else{
                aEnd.next = new ListNode(cur.val);
                aEnd = aEnd.next;
            }
            cur = cur.next;
        }
        bEnd.next = aStart.next;
        return bStart.next;
    }
}

4. 翻转链表(迭代与递归两种方法)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        return reverse(null,head);
    }
     public static ListNode reverse(ListNode prev,ListNode cur) {
        if(cur == null){
            return prev;
        }
        ListNode curNext = cur.next;
        cur.next = prev;
        return reverse(cur,curNext);
    }

}
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
        }
        return pre;
    }
}

*** 5. 删除链表中的相同元素

/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
   public ListNode deleteDuplication(ListNode pHead){
                if (pHead==null || pHead.next==null){return pHead;}
        ListNode Head = new ListNode(0);
        Head.next = pHead;
        ListNode pre  = Head;
        ListNode last = Head.next;
        while (last!=null){
            if(last.next!=null && last.val == last.next.val){
                // 找到最后的一个相同节点
                while (last.next!=null && last.val == last.next.val){
                    last = last.next;
                }
                pre.next = last.next;
                last = last.next;
            }else{
                pre = pre.next;
                last = last.next;
            }
        }
        return Head.next;
    }
}

6. 链表的回文结构

import java.util.*;

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class PalindromeList {
    public boolean chkPalindrome(ListNode A) {
       //如果链表为空,直接反回true,如果链表只有一个节点,直接返回true
        if (A == null){
            return false;
        }else if (A.next == null){
            return true;
        }
        ListNode slow = A;
        ListNode fast = A;
        while (fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
     
        ListNode p = slow.next;
        ListNode p1 = p.next;
        //将后半部分的链表逆置
        while (p != null){
            p.next = slow;
            slow = p;
            p = p1;
            if (p1 != null) {
                p1 = p1.next;
            }
        }
        while(slow != A) {
            if (slow.val != A.val){
                return false;
            }
            //偶数节点
            if (A.next == slow){
                return true;
            }
            A = A.next;
            slow = slow.next;
        }
        return true;
    }
}

7. 判断链表是否有环

/**
 * 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) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){
                return true;
            }
        }
        return false;
    }
}

8. 返回环起始位置

/**
 * 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) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;  
            if(slow == fast){
                slow = head;
                while(slow != fast){
                    slow = slow.next;
                    fast = fast.next;
                }
                return slow;
            }
       }
       return null;  
    }
}

9.相交链表

/**
 * 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;
        }
        int alen = getLength(headA);
        int blen = getLength(headB);
        if (alen < blen) {
            for (int i = 0; i < blen - alen; i++) {
                headB = headB.next;
            }
        } else {
            for (int i = 0; i < alen - blen; i++) {
                headA = headA.next;
            }
        }
        while (headA != headB) {
            headA = headA.next;
            headB = headB.next;
        }
        return headB;
    }

    public static int getLength(ListNode tmp) {
        int cnt = 0;
        while (tmp != null) {
            cnt++;
            tmp = tmp.next;
        }
        return cnt;
    }
}

10.合并两个有序链表

/**
 * 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;
        }
        int alen = getLength(headA);
        int blen = getLength(headB);
        if (alen < blen) {
            for (int i = 0; i < blen - alen; i++) {
                headB = headB.next;
            }
        } else {
            for (int i = 0; i < alen - blen; i++) {
                headA = headA.next;
            }
        }
        while (headA != headB) {
            headA = headA.next;
            headB = headB.next;
        }
        return headB;
    }

    public static int getLength(ListNode tmp) {
        int cnt = 0;
        while (tmp != null) {
            cnt++;
            tmp = tmp.next;
        }
        return cnt;
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我顶得了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值