-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelete Node in a Linked List.py
39 lines (30 loc) · 1.23 KB
/
Delete Node in a Linked List.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# https://leetcode.com/problems/delete-node-in-a-linked-list/
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
"""
OBJECTIVE: Remove node from linked list
Time Complexity: O(n) where n = # of remaining nodes inside the linked list from the given node's
position. The linked list is iterated through from node's position.
Space Complexity: O(1) because no additional space was needed
"""
# Iterate linked list
while node.next.next != None:
# Swap values with next node
oldVal = node.val
node.val = node.next.val
node.next.val = oldVal
# Move to next node
node = node.next
# Move value from next node to this node
# NOTE: At this point, node is behind the last node of the linked list
node.val = node.next.val
node.next = None