Skip to content

Commit f6c74fa

Browse files
committed
Linked list
1 parent e8fe851 commit f6c74fa

14 files changed

+615
-56
lines changed

.idea/workspace.xml

Lines changed: 246 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Merge two sorted linked lists and return it as a new list.
3+
The new list should be made by splicing together the nodes of the first two lists, and should also be sorted.
4+
5+
For example, given following linked lists :
6+
7+
5 -> 8 -> 20
8+
4 -> 11 -> 15
9+
The merged list should be :
10+
11+
4 -> 5 -> 8 -> 11 -> 15 -> 20
12+
"""
13+
14+
from Python.Level4.LinkedList import Node, Traverse
15+
16+
17+
class Solution:
18+
19+
def merge(self, h1, h2):
20+
21+
p1 = h1
22+
p2 = h2
23+
new_head = None
24+
25+
while (p1 is not None) and (p2 is not None):
26+
27+
if p1.val < p2.val:
28+
e = Node(p1.val)
29+
e.next = new_head
30+
new_head = e
31+
p1 = p1.next
32+
else:
33+
e = Node(p2.val)
34+
e.next = new_head
35+
new_head = e
36+
p2 = p2.next
37+
38+
while p1 is not None:
39+
e = Node(p1.val)
40+
e.next = new_head
41+
new_head, p1 = e, p1.next
42+
43+
while p2 is not None:
44+
e = Node(p2.val)
45+
e.next = new_head
46+
new_head, p2 = e, p2.next
47+
48+
# Reversing the list
49+
prev = None
50+
current = new_head
51+
while current is not None:
52+
nx = current.next
53+
current.next = prev
54+
prev = current
55+
current = nx
56+
new_head = prev
57+
58+
return new_head
59+
60+
61+
"""Testing Code """
62+
63+
if __name__ == "__main__":
64+
# initializing the linked list values
65+
h1, h1.next, h1.next.next = Node(1), Node(2), Node(3)
66+
h2, h2.next, h2.next.next, h2.next.next.next = Node(4), Node(5), Node(6), Node(7)
67+
# printing the new list
68+
Traverse().print_list(Solution().merge(h1, h2))
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
Given a singly linked list, determine if its a palindrome. Return 1 or 0 denoting if its a palindrome or not,
3+
respectively.
4+
5+
Notes:
6+
7+
Expected solution is linear in time and constant in space.
8+
For example,
9+
10+
List 1-->2-->1 is a palindrome.
11+
List 1-->2-->3 is not a palindrome.
12+
"""
13+
14+
from Python.Level4.LinkedList import Node, Traverse
15+
16+
17+
class Solution:
18+
19+
def is_palindrome(self, head):
20+
"""
21+
Using list :
22+
append the value of linked list in a python array and checking if array and reverse of array is equal or not
23+
:param head:
24+
:return: isPalindrome
25+
"""
26+
27+
if not head or head.next is None:
28+
return True
29+
30+
is_palindrome = 0
31+
32+
values = []
33+
34+
while head is not None:
35+
values.append(head.val)
36+
head = head.next
37+
38+
if values == values[::-1]:
39+
is_palindrome = 1
40+
41+
return is_palindrome
42+
43+
def is_palindrome_02(self, head):
44+
""" Create a list by traversing the current list value
45+
Traverse both list :::
46+
if all values are equal then return 1
47+
else return 0
48+
"""
49+
x = []
50+
p = head
51+
while p is not None:
52+
x.append(p.val)
53+
p = p.next
54+
i = len(x) - 1
55+
while head is not None and i > -1:
56+
if head.val != x[i]:
57+
return 0
58+
else:
59+
head = head.next
60+
i -= 1
61+
return 1
62+
63+
def is_palindrome_03(self, head):
64+
reverse, fast = None, head
65+
# Reverse the first half part of the list.
66+
while fast and fast.next:
67+
fast = fast.next.next
68+
head.next, reverse, head = reverse, head, head.next
69+
70+
# If the number of the nodes is odd,
71+
# set the head of the tail list to the next of the median node.
72+
tail = head.next if fast else head
73+
74+
# Compare the reversed first half list with the second half list.
75+
# And restore the reversed first half list.
76+
is_palindrome = 1
77+
while reverse:
78+
is_palindrome = is_palindrome and reverse.val == tail.val
79+
reverse.next, head, reverse = head, reverse, reverse.next
80+
tail = tail.next
81+
82+
return is_palindrome
83+
84+
85+
86+
if __name__ == "__main__":
87+
# initializing the linked list values
88+
# h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next = Node(1), Node(4), Node(6), Node(4), Node(1)
89+
h2, h2.next, h2.next.next = Node(1), Node(2), Node(1)
90+
# printing the new list
91+
# Traverse().print_list(Solution().is_palindrome(h1))
92+
print(Solution().is_palindrome_02(h2))
93+
# print(Solution().is_palindrome(h2))
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Given a linked list, remove the nth node from the end of list and return its head.
3+
4+
For example,
5+
Given linked list: 1->2->3->4->5, and n = 2.
6+
After removing the second node from the end, the linked list becomes 1->2->3->5.
7+
8+
Note:
9+
If n is greater than the size of the list, remove the first node of the list.
10+
Try doing it using constant additional space.
11+
"""
12+
from Python.Level4.LinkedList import Node, Traverse
13+
14+
15+
class Solution:
16+
17+
def remove_nth_node(self, head, k):
18+
count = 0
19+
prev = head
20+
curr_ptr = head
21+
temp = head
22+
length = 0
23+
while temp is not None:
24+
length += 1
25+
temp = temp.next
26+
if head is not None:
27+
while count < k:
28+
if prev is None:
29+
head = curr_ptr.next
30+
head.next = curr_ptr.next.next
31+
return head
32+
prev = prev.next
33+
count += 1
34+
if count == length:
35+
head = curr_ptr.next
36+
return head
37+
elif count == 0:
38+
while curr_ptr.next is not None:
39+
prev = curr_ptr
40+
curr_ptr = curr_ptr.next
41+
prev.next = None
42+
return head
43+
else:
44+
while prev.next is not None:
45+
curr_ptr = curr_ptr.next
46+
prev = prev.next
47+
curr_ptr.next = curr_ptr.next.next
48+
return head
49+
50+
51+
if __name__ == "__main__":
52+
# initializing the linked list values
53+
h1, h1.next, h1.next.next, h1.next.next.next = Node(1), Node(2), Node(3), Node(4)
54+
# h2, h2.next, h2.next.next, h2.next.next.next = Node(4), Node(5), Node(6), Node(7)
55+
# printing the new list
56+
Traverse().print_list(Solution().remove_nth_node(h1, 6))
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Given a singly linked list
3+
4+
L: L0 → L1 → … → Ln-1 → Ln,
5+
reorder it to:
6+
7+
L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …
8+
You must do this in-place without altering the nodes’ values.
9+
10+
For example,
11+
Given {1,2,3,4}, reorder it to {1,4,2,3}.
12+
13+
"""
14+
from Python.Level4.LinkedList import Node, Traverse
15+
16+
17+
class Solution:
18+
19+
def reorder_list(self, head):
20+
21+
ptr1 = head
22+
count = 0
23+
temp = head
24+
while temp is not None:
25+
count += 1
26+
temp = temp.next
27+
i = 0
28+
new_head = head
29+
pre = head
30+
while i < (count // 2):
31+
pre = new_head
32+
new_head = new_head.next
33+
i += 1
34+
pre.next = None
35+
ptr3 = None
36+
while ptr1 is not None:
37+
ptr2 = new_head
38+
while ptr2.next is not None:
39+
ptr3 = ptr2
40+
ptr2 = ptr2.next
41+
ptr4 = ptr1.next
42+
ptr1.next = ptr2
43+
ptr2.next = ptr4
44+
if ptr3 is not None:
45+
ptr3.next = None
46+
ptr1 = ptr4
47+
res = head
48+
if count % 2 != 0:
49+
while res.next is not None:
50+
res = res.next
51+
res.next = ptr3
52+
return head
53+
54+
55+
if __name__ == "__main__":
56+
# initializing the linked list values
57+
# h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next, h1.next.next.next.next.next = Node(1), Node(
58+
# 2), Node(3), Node(4), Node(5), Node(6)
59+
# h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next = Node(1), Node(2), Node(3), Node(4), Node(5)
60+
h1, h1.next,h1.next.next = Node(1), Node(2), Node(3)
61+
# h2, h2.next, h2.next.next = Node(1), Node(2), Node(1)
62+
# printing the new list
63+
Traverse().print_list(Solution().reorder_list(h1))
64+
# print(Solution().is_palindrome_02(h2))
65+
# print(Solution().is_palindrome(h2))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Reverse a linked list from position m to n. Do it in-place and in one-pass.
3+
4+
For example:
5+
Given 1->2->3->4->5->6->NULL, m = 2 and n = 4,
6+
7+
return 1->4->3->2->5->NULL.
8+
"""
9+
10+
from Python.Level4.LinkedList import Node, Traverse
11+
12+
13+
class Solution:
14+
15+
def reverse_list(self, head, m, n):
16+
17+
diff, dummy, cur = n - m + 1, Node(-1), head
18+
dummy.next = head
19+
20+
last_unswapped = dummy
21+
while cur and m > 1:
22+
cur, last_unswapped, m = cur.next, cur, m - 1
23+
24+
prev, first_swapped = last_unswapped, cur
25+
while cur and diff > 0:
26+
cur.next, prev, cur, diff = prev, cur, cur.next, diff - 1
27+
28+
last_unswapped.next, first_swapped.next = prev, cur
29+
30+
return dummy.next
31+
32+
33+
if __name__ == "__main__":
34+
# initializing the linked list values
35+
h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next, h1.next.next.next.next.next = Node(1), Node(
36+
2), Node(3), Node(4), Node(5), Node(6)
37+
# h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next = Node(1), Node(2), Node(3), Node(4), Node(5)
38+
# h1, h1.next, h1.next.next = Node(1), Node(2), Node(3)
39+
# h2, h2.next, h2.next.next = Node(1), Node(2), Node(1)
40+
# printing the new list
41+
Traverse().print_list(Solution().reverse_list(h1, 2, 4))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Given a list, rotate the list to the right by k places, where k is non-negative.
3+
4+
For example:
5+
6+
Given 1->2->3->4->5->NULL and k = 2,
7+
return 4->5->1->2->3->NULL.
8+
"""
9+
from Python.Level4.LinkedList import Node, Traverse
10+
11+
12+
class Solution:
13+
14+
def rotate_list(self, head, k):
15+
16+
if not head or not head.next:
17+
return head
18+
count = 1
19+
curr = head
20+
while curr.next is not None:
21+
count += 1
22+
curr = curr.next
23+
curr.next = head
24+
25+
curr, last, i = head, curr, 0
26+
27+
while i < (count - k % count):
28+
last = curr
29+
curr = curr.next
30+
i += 1
31+
last.next = None
32+
return curr
33+
34+
35+
if __name__ == "__main__":
36+
# initializing the linked list values
37+
h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next, h1.next.next.next.next.next = Node(1), Node(
38+
2), Node(3), Node(4), Node(5), Node(6)
39+
# h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next = Node(1), Node(2), Node(3), Node(4), Node(5)
40+
# h1, h1.next, h1.next.next = Node(1), Node(2), Node(3)
41+
# h2, h2.next, h2.next.next = Node(1), Node(2), Node(1)
42+
# printing the new list
43+
Traverse().print_list(Solution().rotate_list(h1, 89))
44+
# print(Solution().is_palindrome_02(h2))
45+
# print(Solution().is_palindrome(h2))

Python/Level4/LinkedList/ListSort/Insertion Sort List.py

Whitespace-only changes.

Python/Level4/LinkedList/ListSort/Partition List.py

Whitespace-only changes.

Python/Level4/LinkedList/ListSort/Sort List.py

Whitespace-only changes.

Python/Level4/LinkedList/ListSort/__init__.py

Whitespace-only changes.

Python/Level4/LinkedList/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ def print_list(self, head):
2424
while p is not None:
2525
print(p.val, end="->")
2626
p = p.next
27+
print("\n")
Binary file not shown.

Python/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)