链表

链表类题目

(24)两两交换链表中的节点-中等

题目

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

思路1:常规迭代

代码1

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode newHead = new ListNode(-1, head);
        ListNode i = head, before = newHead;
        if(i == null) return null;
        while(i != null && i.next != null){
            before.next = i.next;//前面的指向i后面的
            i.next = i.next.next;//i指向i后面的第二个
            before.next.next = i;

            before = before.next.next;
            i = before.next;
        }
        return newHead.next;
    }
}

思路2:递归

代码2

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {
        //递归出口
        if(head == null || head.next == null)
            return head;
        ListNode next = head.next;
        ListNode temp = next.next;
        next.next = head;
        head.next = swapPairs(temp);
        return next;
    }
}

(61)旋转链表-中等

题目

给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数

思路1:迭代

每旋转一次即为:尾结点指向头结点,倒数第二个结点成新的尾结点,头结点变为之前的尾结点。旋转 k%链表长度 次。

代码1

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        ListNode newHead = head, last1 = null, last2 = null;
        if(head == null || head.next == null) return head;
        int len = 0;
        ListNode temp = head;
        //缩小k的值,旋转 k%链表长度 次即可。
        while(temp != null){
            len++;
            temp = temp.next;
        }
        k = k % len;
        
        for(int i = 0; i < k; i++){
            temp = newHead;
            //找到新的倒数第二个结点
            while(temp.next.next != null) temp = temp.next;
            last2 = temp;//倒数第二个结点
            last1 = last2.next;//倒数第一个

            last1.next = newHead;
            last2.next = null;

            newHead = last1;
        }
        return newHead;
    }
}

思路2:将问题转化为:先形成闭环,再找到断开位置与新头结点。

断开位置:倒数第 k%链表长度 个结点,即正数第len-K个结点;新头结点:断开位置下一个。

代码2

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        ListNode newHead = head;
        if(head == null || head.next == null) return head;
        int len = 1;
        ListNode last = head;
        while(last.next != null){
            len++;
            last = last.next;
        }
        k = k % len;
        if(k == 0) return head;

        //找倒数第k+1个结点,即正数第len-K个结点
        ListNode temp =  head;
        int cnt = len - k;
        for(int i = 0; i < cnt - 1; i++){
            temp = temp.next;
        }

        //形成闭环
        last.next = head;
        //断开该结点并设置新结点
        newHead = temp.next;
        temp.next = null;
        return newHead;
    }
}

(25)K个一组翻转链表-中等

题目

给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。

k 是一个正整数,它的值小于或等于链表的长度。

如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。

思路:递归,借用上一题的递归思路

代码

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        int len, cnt = 1;
        ListNode last = head;
        //特殊情况
        if(k < 1) return head;
        //判断剩余结点数是否大于等于K,last即为第K个结点
        while(last != null && cnt < k){
            last = last.next;
            cnt++;
        }
        //递归出口:第K个结点为null即剩余结点数不足K个
        if(last == null) return head;
        //保留到下一组翻转的头结点
        ListNode nextHead = last.next;
        //将head及后面的共K个结点翻转
        reverse(head, k);
        //现在head已经是翻转后的最后一个结点了,需要指向下一组已经翻转后的头结点
        head.next = reverseKGroup(nextHead, k);
        //返回本组翻转后的头结点,即第K个结点。
        return last;
    }

    //翻转head链表的前k个结点
    public void reverse(ListNode head, int k){
        ListNode before = head, i = head.next;
        ListNode temp = null;
        while(k > 1){
            temp = i.next;
            i.next = before;
            before = i;
            i = temp;
            k--;
        }
    }
}

(82、83)删除排序链表中的重复元素-中等

题目

给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。

思路1:常规迭代

代码1

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode newHead = new ListNode(-1, head), before = newHead, i = head;
        while(i != null && i.next != null){
            int temp = i.val;
            before = i;//需要保留一个放这儿
            if(temp == i.next.val){
                while(i != null && i.val == temp) i = i.next;
                before.next = i;
            }else{
                // before = i;需要去掉重复值一个都不保留放这儿
                i = i.next;
            }
        }
        return newHead.next;
    }
}

思路2:递归

代码2

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        if (head.val == head.next.val) {
            while (head != null && head.next != null && head.val == head.next.val) {
                head = head.next;
            }
            return deleteDuplicates(head.next);
        } else {
            head.next = deleteDuplicates(head.next);
            return head;
        }
    }
}

(86)分隔链表-中等

题目

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

你应当 保留 两个分区中每个节点的初始相对位置。

思路1:常规迭代

遍历链表,小于target的结点就放在最后,大于等于则不变继续往下遍历

代码1

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode newHead = new ListNode(-1, head);
        ListNode before = newHead, i = head, last = head;
        if(head == null || head.next == null) return head;
        int len = 1;
        while(last.next != null){
            last = last.next;
            len++;
        }
        while(len > 0){
            if(i.val < x){
                before = i;
                i = i.next;
            //如果下一个结点为null需要单独考虑,这里刚好不用管
            }else if(i.next != null){
                before.next = i.next;
                last.next = i;
                last = last.next;
                i = i.next;
                last.next = null;
            }
            len--;
        }
        return newHead.next;
    }
}

(86)反转链表-中等

题目

反转从位置 mn 的链表。请使用一趟扫描完成反转。

思路1:递归

递归出口:当left == 1时即到了该翻转的结点位置了,翻转后面的结点return翻转后的第一个结点

比较烦的是:翻转指定的一段链表后我们需要知道两个结点位置 :

1、这段翻转链表后的第一个结点,因为翻转后需要和后面相连 2、这段翻转链表的最后一个结点,因为翻转后需要和前面相连。

于是用了List<ListNode来返回这两个结点。

代码1

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        //特殊情况
        if(head == null || head.next == null || right - left < 1)
            return head;
        // 递归出口
        if(left == 1){
            List<ListNode> arr = reverse(head, right - left + 1);
            //head成为翻转后的最后一个结点,需要连上后面的结点。
            head.next = arr.get(1);
            //返回翻转后的头结点
            return arr.get(0);
        }

        head.next = reverseBetween(head.next, left-1, right-1);
        return head;
    }
    //翻转head链表的前k个结点
    public List<ListNode> reverse(ListNode head, int k){
        List<ListNode> arr = new ArrayList<ListNode>();
        ListNode before = head, i = head.next;
        ListNode temp = null;
        while(k > 1){
            temp = i.next;
            i.next = before;
            before = i;
            i = temp;
            k--;
        }
        arr.add(before);
        arr.add(i);
        return arr;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值