数据结构与算法--链表

LeetCode_206:逆序一个链表

Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

该题目为反转一个链表,首先判断给定的链表head是否为null或者head.next是否为null,如果成立,则不需要翻转此链表,直接返回head即可。
对于长度大于1的链表,声明三个变量:cur、next、tmp,初始化时,令cur指向head,next指向cur的下一个节点,tmp用来临时存放节点。翻转时,先用tmp保存next的下一个节点。

在这里插入图片描述

令next的下一个节点指向cur节点,完成前两个节点的翻转,之后继续往下遍历节点,重复翻转操作,直到cur指向最后一个节点,然后将cur作为头节点返回即可。

在这里插入图片描述

代码:
class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null){
            return head;
        }
        ListNode cur = head;
        ListNode next = cur.next;
        ListNode tmp = null;
        cur.next = null;
        while (next != null){
            tmp = next.next;
            next.next = cur;
            cur = next;
            next = tmp;
        }
        return cur;
    }
}

LeetCode_141:判断链表是否有环

Given a linked list, determine if it has a cycle in it.
Example:
Input: head = [3,2,0,-4], pos = 1
Output: true
在这里插入图片描述

声明两个变量:fast、slow均指向头节点,fast每次往前移动两个节点,slow每次往前移动一个节点,如果这两个节点能走到链表尽头,即fast==null,则说明链表没有环。如果fast和slow在移动过程中能够在某个节点相遇,则说明链表有环。
代码:
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null && slow != null){
            fast = fast.next.next;
            slow = slow.next;
            if (fast != null && fast==slow){
                return true;
            }
        }
        return false;
    }
}

LeetCode_142:判断链表是否有环,如果有,返回环的起始节点。

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

首先利用fast和slow两个节点先判断链表是否有环,如果有环,记录fast和slow相遇的节点。

在这里插入图片描述

假设fast和slow从A点出发,在K点相遇,B点是需要计算的环形起点,因为fast速度是slow的2倍,所以二者在K点相遇时,fast走过的距离是slow的2倍,且fast比slow多走的距离正好是环形的周长L。则有以下等式成立:
ABDK + L = 2 * ABDK
即环形的周长L等于ABDK的长度,那么AB的长度就等于BCK的长度。现在得到了K点的位置,只需用两个节点在A节点和K节点同时出发,每次各移动一步,那么二者相遇的点就是环形的起点B。
代码:
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;
        fast = head;
        while (fast != slow ){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}

LeetCode_24:单链表每两个节点逆序。

Given a linked list, swap every two adjacent nodes and return its head.Example:
Given 1->2->3->4, you should return the list as 2->1->4->3.

在这里插入图片描述

建立一个新节点指向head头节点,每两个节点作为一组,当前组的第一个节点指向下一组的第一个节点,上一组的第二个节点需要指向当前组的第二个节点,当前组的第二个节点指向当前组的第一个节点,依次循环。
代码:
class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode pre = new ListNode(0);
        pre.next = head;
        ListNode result = pre;
        while (head != null && head.next != null){
            ListNode tmp = head.next;
            head.next = tmp.next;
            pre.next = tmp;
            tmp.next = head;
            pre = pre.next.next;
            head = head.next;
        }
        return result.next;
    }
}

LeetCode_25:单链表每K个节点逆序。

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
Example:
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5

首先遍历链表的节点,每k个节点为一组进行操作。记录下当前组的前一个节点和后一个节点,以及组内的开始节点和结束节点。

在这里插入图片描述

对于组内的节点,两两之间进行逆序操作,逆序操作之后,将上一组的尾节点指向当前组的头节点,将当前组的尾节点指向下一组的头节点。

在这里插入图片描述

代码:
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode pre = null;
        ListNode start = null;
        int count = 1;
        ListNode cur = head;
        ListNode next = null;
        while (cur != null){
            next = cur.next;
            if (count == k){
                start = pre == null ? head : pre.next;
                head = pre == null ? cur : head;
                resign(pre,start,cur,next);
                pre = start;
                count = 0;
            }
            count ++;
            cur = next;
        }
        return head;
    }
    public void resign(ListNode left,ListNode start,ListNode end,ListNode right){
        ListNode pre = start;
        ListNode cur = start.next;
        ListNode next = null;
        while (cur != right){
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        if (left != null){
            left.next = end;
        }
        start.next = right;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值