Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 54eaf01

Browse files
authored
Add files via upload
1 parent 32e0e54 commit 54eaf01

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
class LinkedListNode:
2+
# __init__ will be used to make a LinkedListNode type object.
3+
def __init__(self, data, next=None):
4+
self.data = data
5+
self.next = next
6+
7+
8+
class LinkedList:
9+
# __init__ will be used to make a LinkedList type object.
10+
def __init__(self):
11+
self.head = None
12+
13+
# insert_node_at_head method will insert a LinkedListNode at head
14+
# of a linked list.
15+
def insert_node_at_head(self, node):
16+
if self.head:
17+
node.next = self.head
18+
self.head = node
19+
else:
20+
self.head = node
21+
22+
# create_linked_list method will create the linked list using the
23+
# given integer array with the help of InsertAthead method.
24+
def create_linked_list(self, lst):
25+
for x in reversed(lst):
26+
new_node = LinkedListNode(x)
27+
self.insert_node_at_head(new_node)
28+
29+
# __str__(self) method will display the elements of linked list.
30+
def __str__(self):
31+
result = ""
32+
temp = self.head
33+
while temp:
34+
result += str(temp.data)
35+
temp = temp.next
36+
if temp:
37+
result += ", "
38+
result += ""
39+
return result
40+
41+
42+
def print_list_with_forward_arrow(linked_list_node):
43+
temp = linked_list_node
44+
while temp:
45+
print(temp.data, end=" ") # print node value
46+
47+
temp = temp.next
48+
if temp:
49+
print("→", end=" ")
50+
else:
51+
# if this is the last node, print null at the end
52+
print("→ null", end=" ")
53+
54+
55+
def reverse_linked_list(head):
56+
prev, curr = None, head
57+
while curr:
58+
nxt = curr.next
59+
curr.next = prev
60+
prev = curr
61+
curr = nxt
62+
return prev
63+
64+
65+
def traverse_linked_list(head):
66+
current, nxt = head, None
67+
while current:
68+
nxt = current.next
69+
current = nxt
70+
71+
72+
73+
############################################################
74+
75+
76+
77+
def swap_pairs(head):
78+
79+
# Verify at least 2 nodes exist
80+
if not head or not head.next:
81+
return head
82+
83+
# Initialize dummy node and pointers
84+
prev = LinkedListNode(0) # Dummy node to handle the head
85+
prev.next = head
86+
current = head
87+
new_head = current.next
88+
89+
# Traverse LinkedList
90+
while current and current.next:
91+
92+
# Obtain the two nodes that need to be swapped
93+
first = current
94+
second = current.next
95+
96+
# Swap the nodes
97+
first.next = second.next
98+
second.next = first
99+
100+
# Link previous node to second node
101+
prev.next = second
102+
103+
# Iterate ahead to the next pair of nodes
104+
prev = first
105+
current = first.next
106+
107+
# Replace this placeholder return statement with your code
108+
return new_head
109+
110+
111+
112+
# Time Complexity = O(n)
113+
# Space Complexity = O(1)
114+
115+
116+
117+
############################################################
118+
119+
120+
121+
# Driver Cod
122+
def main():
123+
test_cases = [
124+
[10, 1, 2, 3, 4, 5],
125+
[28, 21, 14, 7],
126+
[11, 12, 13, 14, 15],
127+
[1, 2]
128+
]
129+
130+
for idx, test_case in enumerate(test_cases):
131+
print(f"Test case {idx + 1}: {test_case}")
132+
133+
# Creating a linked list
134+
linked_list = LinkedList()
135+
linked_list.create_linked_list(test_case)
136+
137+
# Printing the original linked list
138+
print("Original list: ", end="")
139+
print_list_with_forward_arrow(linked_list.head)
140+
print() # To add a newline for clarity
141+
142+
# Swapping pairs
143+
swapped_head = swap_pairs(linked_list.head)
144+
145+
# Printing the swapped linked list
146+
print("Swapped list: ", end="")
147+
print_list_with_forward_arrow(swapped_head)
148+
print("\n") # To separate test cases
149+
150+
151+
if __name__ == '__main__':
152+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /