刷题记录|目标101题--链表

写在前面

开个贴记录一下 刷题的过程,目前参照 leetcode101,目标101题,由于太久没写java了,也会涉及到一些java的基础用法,希望能和大家共勉吧~

已刷链接:

链表

No.1 Reverse Linked List

题目:https://leetcode.com/problems/reverse-linked-list/

非递归:

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

递归:

/**
 * 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 reverseList(ListNode head) {
        ListNode previous = null;
        return reverse(head,previous);
    }
    private ListNode reverse(ListNode head, ListNode pre) {
        if (head == null) {
            return pre;
        }
        ListNode next = head.next;
        head.next = pre;
        return reverse(next,head);
    }
}

No.2 Merge Two Sorted Lists

题目:https://leetcode.com/problems/merge-two-sorted-lists/

非递归:

/**
 * 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 mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode mergeList = new ListNode(0);
        ListNode current = mergeList;
        while(list1 != null && list2 != null) {
            if (list1.val <= list2.val) {
                current.next = list1;
                list1 = list1.next;
            } else {
                current.next = list2;
                list2 = list2.next;
            }
            current = current.next;
        }
        current.next = list1 == null ? list2 : list1;
        return mergeList.next;
    }
}

递归:

/**
 * 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 mergeTwoLists(ListNode list1, ListNode list2) {
        if (list2 == null) return list1;
        if (list1 == null) return list2;
        if (list1.val <= list2.val) {
            list1.next = mergeTwoLists(list1.next,list2);
            return list1;
        } else {
            list2.next = mergeTwoLists(list1, list2.next);
            return list2;
        }
    }
}

No.3 Swap Nodes in Pairs

题目:https://leetcode.com/problems/swap-nodes-in-pairs/

解题思路:

注意第一次交换不用链接previous,但是后续需要链接previous

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

No.4 Intersection of Two Linked Lists

题目:https://leetcode.com/problems/intersection-of-two-linked-lists/submissions/

解题思路:

令A到相交处的长度为a,B到相交处的长度为b,相交处到链表尾部的长度为c。a和b开头各一个指针,以相同的速度行走,走到头以后交换至另一个链表头,有走过的长度a+c+b = b+c+a,会在c1处相遇。

记得要处理不相交的情况,所以要允许node走到结尾为null的情况,不然会死循环。

/**
 * 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;
        ListNode curA = headA, curB = headB;
        while (curA != curB) {
            if (curA != null) {
                curA = curA.next;
            } else {
                curA = headB;
            }
            if (curB != null) {
                curB = curB.next;
            } else {
                curB = headA;
            }
        }
        return curA;
    }
}

No.5 Palindrome Linked List

题目:https://leetcode.com/problems/palindrome-linked-list/

算法解析:

先快慢指针求出中点,然后反转后半部分list,反转后进行对比

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

No.6 Remove Duplicates from Sorted List

题目:https://leetcode.com/problems/remove-duplicates-from-sorted-list/

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

No.7 Odd Even Linked List

题目:https://leetcode.com/problems/odd-even-linked-list/

题目解析:

/**
 * 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 oddEvenList(ListNode head) {
        if (head == null || head.next == null || head.next.next == null) return head;
        ListNode even = new ListNode(0);
        ListNode cur = head, ec = even, pre = null;
        while (cur != null && cur.next != null) {
            ec.next = cur.next;
            cur.next = cur.next.next;
            ec = ec.next;
            ec.next = null;
            pre = cur;
            cur = cur.next;
        }
        if (cur == null) {
            pre.next = even.next;
        } else {
            cur.next = even.next;
        }
        return head;
    }
}

No.8 Remove Nth Node From End of List

题目:https://leetcode.com/problems/remove-nth-node-from-end-of-list/

解题思路:

利用快慢指针,让fast指针比slow指针快n个,然后再同步走,这样slow和fast之间永远差n,当fast到底时slow就在可以删除的位置

/**
 * 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 removeNthFromEnd(ListNode head, int n) {
        ListNode pre = new ListNode(0);
        pre.next = head;
        ListNode fast = pre,slow = pre;
        for (int i = 0; i < n; i ++) {
            fast = fast.next;
        }
        while (fast != null && fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return pre.next;
    }
}

No. 9 Sort List

题目:https://leetcode.com/problems/sort-list/

思路解析:

先用快慢指针求出中点,然后用使用归并排序。注意列表的排序经常使用归并排序

/**
 * 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 sortList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode fast = head, slow = head, temp = null;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            temp = slow;
            slow = slow.next;
        }
        temp.next = null;
        ListNode left = sortList(head);
        ListNode right = sortList(slow);
        return mergeSort(left, right);
    }
    private ListNode mergeSort(ListNode l, ListNode r) {
        ListNode res = new ListNode();
        ListNode p = res;
        while (l != null && r != null) {
            if (l.val < r.val) {
                p.next = l;
                l = l.next;
            } else {
                p.next = r;
                r = r.next;
            }
            p = p.next;
        }
        p.next = l == null ? r : l;
        return res.next;
    }
}

#刷题##逃离互联网#
全部评论

相关推荐

昨天 11:58
门头沟学院 Java
腾讯暑期实习java选手,完全不懂C++,貌似游戏行业都是用C++的而且天美好像在成都,个人比较想去上海或深圳
siestaaaaaa:天美不止在成都,深圳上海都有。 游戏服务器也不全是cpp,至少我们项目是java ,但是工作中什么语言都会用到,比如cpp、lua、py等等,语言本身其实不重要
点赞 评论 收藏
分享
03-02 16:31
已编辑
合肥工业大学 golang
程序员鼠鼠_春招版:学历可以,项目普通,评价多余,奖项没有,如果有面试都是因为学历给你的,我建议可以随便包几个奖项上去,像什么蓝桥杯天梯赛,虽然不一定有用,但是相比acm这种风险小多了,我几段实习下来,压根没查的,第二点是包一段小厂实习,大厂你不好拿捏,小厂打打杂也能让你在26里面出彩一点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务