摘要: 双指针法 class Solution { public boolean isPalindrome(ListNode head) { ListNode dummyHead = new ListNode(0, head); ListNode slow = dummyHead; ListNode fas 阅读全文
posted @ 2021-12-19 18:29 振袖秋枫问红叶 阅读(34) 评论(0) 推荐(0)
摘要: class Solution { public void reorderList(ListNode head) { ListNode dummyHead = new ListNode(0, head); ListNode slow = dummyHead; ListNode fast = dummy 阅读全文
posted @ 2021-12-19 17:43 振袖秋枫问红叶 阅读(33) 评论(0) 推荐(0)
摘要: 闭合为环 class Solution { public ListNode rotateRight(ListNode head, int k) { if (head == null || head.next == null || k == 0){ return head; } ListNode du 阅读全文
posted @ 2021-12-19 16:03 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要: 两次遍历 class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { /** * 两次遍历 * 先得到链表的长度,再正向遍历到length - n的位置,也就是待删除节点的前一个节点 */ ListNode du 阅读全文
posted @ 2021-12-19 15:05 振袖秋枫问红叶 阅读(46) 评论(0) 推荐(0)
摘要: 和下一个节点交换 class Solution { public void deleteNode(ListNode node) { node.val = node.next.val; node.next = node.next.next; } } /** * 时间复杂度 O(1) * 空间复杂度 O 阅读全文
posted @ 2021-12-19 14:39 振袖秋枫问红叶 阅读(48) 评论(0) 推荐(0)
摘要: 自顶向下(递归) class Solution { public ListNode sortList(ListNode head) { return sortList(head, null); } public ListNode sortList(ListNode head, ListNode ta 阅读全文
posted @ 2021-12-19 14:23 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)