blob: c7fc98970390fc5ec4de248abd3f1be6dbdb98ff (
plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
#!/usr/bin/env python3
# Challenge 071
#
# TASK #2 > Trim Linked List
# Submitted by: Mohammad S Anwar
# You are given a singly linked list and a positive integer $N (>0).
#
# Write a script to remove the $Nth node from the end of the linked list and
# print the linked list.
#
# If $N is greater than the size of the linked list then remove the first
# node of the list.
#
# NOTE: Please use pure linked list implementation.
#
# Example
# Given Linked List: 1 -> 2 -> 3 -> 4 -> 5
# when $N = 1
# Output: 1 -> 2 -> 3 -> 4
# when $N = 2
# Output: 1 -> 2 -> 3 -> 5
# when $N = 3
# Output: 1 -> 2 -> 4 -> 5
# when $N = 4
# Output: 1 -> 3 -> 4 -> 5
# when $N = 5
# Output: 2 -> 3 -> 4 -> 5
# when $N = 6
# Output: 2 -> 3 -> 4 -> 5
import sys
def list_len(lst):
length = 0
while lst:
length += 1
lst = lst[1]
return length
def remove_n(remove, lst):
if remove == 0:
return lst[1]
else:
p = lst
last = None
while p:
if remove == 0:
last[1] = p[1]
last = p
p = p[1]
remove -= 1
return lst
def show(lst):
out = []
while lst:
out.append(lst[0])
lst = lst[1]
print(" ".join(map(str, out)))
n = int(sys.argv[1]) if len(sys.argv) > 1 else 1
lst = None
for arg in reversed(sys.argv[2:]):
lst = [arg, lst]
length = list_len(lst)
remove = 0 if n > length else length - n
lst = remove_n(remove, lst)
show(lst)
|