输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。例如,一个链表有6个节点,从头节点开始,它们的值依次是一、二、三、四、五、6。这个链表的倒数第3个节点是值为4的节点。java
来源:力扣(LeetCode)
连接:https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof
著做权归领扣网络全部。商业转载请联系官方受权,非商业转载请注明出处。node
示例:数组
给定一个链表: 1->2->3->4->5, 和 k = 2. 返回链表 4->5
public ListNode getKthFromEnd(ListNode head, int k) { if (k == 0) return null; ListNode node = new ListNode(0, null); ListNode curr = node; while (head != null) { ListNode temp = head.next; curr.next = head; curr = curr.next; curr.next = null; head = temp; k--; if (k < 0) node = node.next; } return node.next; }
设置快慢指针,两个指针之间正好有k个元素,遍历head的时候两个指针分别加一,遍历结束后,返回慢指针便可网络
public ListNode getKthFromEnd(ListNode head, int k) { if (k == 0) return null; ListNode next = head; for (int i = 0; i < k - 1; i++) { next = next.next; } while (next.next != null) { head = head.next; next = next.next; } return head; }
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。函数
返回删除后的链表的头节点。指针
示例 1:code
输入: head = [4,5,1,9], val = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数以后,该链表应变为 4 -> 1 -> 9.
示例 2:对象输入: head = [4,5,1,9], val = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数以后,该链表应变为 4 -> 5 -> 9.排序来源:力扣(LeetCode)
连接:https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof
著做权归领扣网络全部。商业转载请联系官方受权,非商业转载请注明出处。递归
public ListNode deleteNode(ListNode head, int val) { if (head.val == val) { head = head.next; return head; } ListNode curr = head; while (curr != null && curr.next != null) { if (curr.next.val == val) { curr.next = curr.next.next; } curr = curr.next; } return head; }
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
示例1:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
限制:0 <= 链表长度 <= 1000
注意:本题与主站 21 题相同:https://leetcode-cn.com/problems/merge-two-sorted-lists/
来源:力扣(LeetCode)
连接:https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof
著做权归领扣网络全部。商业转载请联系官方受权,非商业转载请注明出处。
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode node = new ListNode(0,null); ListNode curr = node; while (l1 != null && l2 != null) { if (l1.val <= l2.val) { ListNode temp = l1.next; curr.next = l1; curr = curr.next; curr.next = null; l1 = temp; } else { ListNode temp = l2.next; curr.next = l2; curr = curr.next; curr.next = null; l2 = temp; } } if (l1 != null) { curr.next = l1; } else { curr.next = l2; } return node.next; }
利用递归思想,递归终止条件就是其中一条链表为空,而后返回另外一条便可
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { //递归终止条件 if (l1 == null) { return l2; } if (l2 == null) { return l1; } //不停递归 if (l1.val < l2.val ) { l1.next = mergeTwoLists(l1.next,l2); return l1; } else { l2.next = mergeTwoLists(l2.next,l1); return l2; } }
给定一个带有头结点 head
的非空单链表,返回链表的中间结点。
若是有两个中间结点,则返回第二个中间结点。
示例 1:
输入:[1,2,3,4,5]
输出:此列表中的结点 3 (序列化形式:[3,4,5])
返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
注意,咱们返回了一个 ListNode 类型的对象 ans,这样:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.
示例 2:输入:[1,2,3,4,5,6]
输出:此列表中的结点 4 (序列化形式:[4,5,6])
因为该列表有两个中间结点,值分别为 3 和 4,咱们返回第二个结点。提示:
给定链表的结点数介于 1 和 100 之间。
来源:力扣(LeetCode)
连接:https://leetcode-cn.com/problems/middle-of-the-linked-list
著做权归领扣网络全部。商业转载请联系官方受权,非商业转载请注明出处。
单链表不容易判断中间节点的位置,因此将链表遍历转换为数组
时间复杂度:O(n)
空间复杂度:O(n)
public ListNode middleNode(ListNode head) { ListNode[] nodes = new ListNode[100]; int count = 0; while (head != null) { nodes[count++] = head; head = head.next; } return nodes[count / 2]; }
将链表循环遍历一遍,记录总共多少个节点,以后求出中间节点的位置,再次遍历链表
时间复杂度:O(n)
空间复杂度:O(1)
public ListNode middleNode(ListNode head) { ListNode curr = head; int count = 0; while (curr != null) { count++; curr = curr.next; } int mid = count / 2; for (int i = 0; i < mid; i++) { head = head.next; } return head; }
快指针每次走两步,慢指针每次走一步,当快指针到达链表末尾时,慢指针就位于中间位置
时间复杂度:O(n)
空间复杂度:O(1)
public ListNode middleNode3(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; }