摘要: 思路: 快慢指针找到中间节点,切成两半。(注意链表长度的奇偶性) 后半部分 reverse 操作。 归并操作,即后半部分 塞到 前半部分的“缝隙”里,组成新的链表。 class Solution { public void reorderList(ListNode head) { if (head 阅读全文
posted @ 2020-12-14 22:26 不学无墅_NKer 阅读(33) 评论(0) 推荐(0) 编辑
摘要: 思路:同剑指14.链表中倒数第k个结点 class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { /** * 方法1:计算链表长度,相当于两次遍历链表 */ /* ListNode dummyHead = ne 阅读全文
posted @ 2020-12-14 21:00 不学无墅_NKer 阅读(37) 评论(0) 推荐(0) 编辑
摘要: ☆☆思路:改变链表节点的值。首先将下一个节点的值赋给node,然后直接跳过下一个节点(删除下一个节点)。 class Solution { public void deleteNode(ListNode node) { node.val = node.next.val; node.next = no 阅读全文
posted @ 2020-12-14 20:14 不学无墅_NKer 阅读(78) 评论(0) 推荐(0) 编辑
摘要: ☆☆☆☆【字节】 很综合的一道题目,用到 [LeetCode206. 反转链表] class Solution { public ListNode reverseKGroup(ListNode head, int k) { if (head == null || head.next == null) 阅读全文
posted @ 2020-12-14 17:10 不学无墅_NKer 阅读(71) 评论(0) 推荐(0) 编辑
摘要: 方法1: class Solution { public ListNode swapPairs(ListNode head) { if (head == null || head.next == null) return head; ListNode dummyHead = new ListNode 阅读全文
posted @ 2020-12-14 15:49 不学无墅_NKer 阅读(104) 评论(0) 推荐(0) 编辑
摘要: 解法:本题同剑指16.合并两个排序的链表 class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { /** * 解法1: 非递归 */ ListNode dummyHead = new ListNode(-1 阅读全文
posted @ 2020-12-14 11:38 不学无墅_NKer 阅读(76) 评论(0) 推荐(0) 编辑
摘要: ☆☆☆☆解法:本题同剑指56.删除链表中重复的结点 class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null || head.next == null) return head; ListN 阅读全文
posted @ 2020-12-14 10:34 不学无墅_NKer 阅读(51) 评论(0) 推荐(0) 编辑
摘要: class Solution { public ListNode removeElements(ListNode head, int val) { if (head == null) return null; ListNode dummyHead = new ListNode(-1); dummyH 阅读全文
posted @ 2020-12-14 09:40 不学无墅_NKer 阅读(33) 评论(0) 推荐(0) 编辑