LeetCode 2095. Delete the Middle Node of a Linked List
原题链接在这里:https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/
题目:
You are given the head
of a linked list. Delete the middle node, and return the head
of the modified linked list.
The middle node of a linked list of size n
is the ⌊n / 2⌋th
node from the start using 0-based indexing, where ⌊x⌋
denotes the largest integer less than or equal to x
.
- For
n
=1
,2
,3
,4
, and5
, the middle nodes are0
,1
,1
,2
, and2
, respectively.
Example 1:
Input: head = [1,3,4,7,1,2,6] Output: [1,3,4,1,2,6] Explanation: The above figure represents the given linked list. The indices of the nodes are written below. Since n = 7, node 3 with value 7 is the middle node, which is marked in red. We return the new list after removing this node.
Example 2:
Input: head = [1,2,3,4] Output: [1,2,4] Explanation: The above figure represents the given linked list. For n = 4, node 2 with value 3 is the middle node, which is marked in red.
Example 3:
Input: head = [2,1] Output: [2] Explanation: The above figure represents the given linked list. For n = 2, node 1 with value 1 is the middle node, which is marked in red. Node 0 with value 2 is the only node remaining after removing node 1.
Constraints:
- The number of nodes in the list is in the range
[1, 105]
. 1 <= Node.val <= 105
题解:
Find the pre node of middle node.
Have walker stop before middle node. The stop condidtion is runner != null && runner.next != null && runner.next.next != null.
Then do walker.next = walker.next.next.
Time Complexity: O(n). n is the length of list.
Space: O(1).
AC Java:
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode() {} 7 * ListNode(int val) { this.val = val; } 8 * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 * } 10 */ 11 class Solution { 12 public ListNode deleteMiddle(ListNode head) { 13 if(head == null){ 14 return head; 15 } 16 17 ListNode dummy = new ListNode(-1); 18 dummy.next = head; 19 ListNode runner = dummy; 20 ListNode walker = dummy; 21 while(runner != null && runner.next != null && runner.next.next != null){ 22 runner = runner.next.next; 23 walker = walker.next; 24 } 25 26 walker.next = walker.next.next; 27 return dummy.next; 28 } 29 }
AC C++:
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode() : val(0), next(nullptr) {} 7 * ListNode(int x) : val(x), next(nullptr) {} 8 * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 * }; 10 */ 11 class Solution { 12 public: 13 ListNode* deleteMiddle(ListNode* head) { 14 if(!head || !head->next){ 15 return NULL; 16 } 17 18 ListNode *dummy = new ListNode(-1, head); 19 ListNode *walker = dummy; 20 ListNode *runner = dummy; 21 while(runner && runner->next && runner->next->next){ 22 runner = runner->next->next; 23 walker = walker->next; 24 } 25 26 walker->next = walker->next->next; 27 return dummy->next; 28 } 29 };
AC Python:
1 # Definition for singly-linked list. 2 # class ListNode: 3 # def __init__(self, val=0, next=None): 4 # self.val = val 5 # self.next = next 6 class Solution: 7 def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]: 8 if not head.next: 9 return None 10 11 dummy = ListNode(-1, head) 12 walker = runner = dummy 13 while(runner and runner.next and runner.next.next): 14 walker = walker.next 15 runner = runner.next.next 16 17 walker.next = walker.next.next 18 return dummy.next
类似Middle of the Linked List, Remove Nth Node From End of List.