My LinkedList and Node class implementation has been stolen from here.
class Node:
def __init__(self, value=None, next=None):
self.value = value
self.next = next
def __str__(self):
return 'Node[' + self.value +']'
class Linked_List:
def __init__(self):
self.first = None
self.last = None
def insert(self, x):
if self.first is None:
self.first = Node(x, None)
self.last = self.first
elif self.last is self.first:
self.last = Node(x,None)
self.first.next = self.last
else:
current = Node(x, None)
self.last.next = current
self.last = current
def __str__(self):
if self.first is not None:
current = self.first
out = 'LinkedList[\n' + str(current.value) + '\n'
while current.next is not None:
current = current.next
out += str(current.value) + '\n'
return out + ']'
def clear(self):
self.__init__()
I want to swap every two nodes in the LinkedList, as an example:
Input: 0 1 2 3 4 5 6 7 8 9
Output: 1 0 3 2 5 4 7 6 9 8
My code to create, and then attempt to swap every two elements in the list, is as follows:
for i in range(10):
if i is 0:
linkedlist = Linked_List()
linkedlist.insert(str(i))
else:
linkedlist.insert(str(i))
current = linkedlist.first
while current is not linkedlist.last:
print(str(current))
print("NEXT =" + str(current.next))
print("NEXT.NEXT=" + str(current.next.next))
if current is linkedlist.first:
linkedlist.first = current.next
current.next, current.next.next = current.next.next, current.next
print(str(current))
print("NEXT =" + str(current.next))
print("NEXT.NEXT=" + str(current.next.next))
current = current.next
The meat of my question:
current.next, current.next.next = current.next.next, current.next
In my mind, this should swap the current node's reference, and the next node's reference. Resulting in the first case:
Node[0].next=Node[2]
Node[1].next=Node[0]
Thus altering the list from/to:
0 1 2 3 4 5 6 7 8 9
1 0 2 3 4 5 6 7 8 9
And it seems to nearly work, as the output from the first iteration of this while
loop is:
Node[0]
NEXT =Node[1]
NEXT.NEXT=Node[2]
Node[0]
NEXT =Node[2]
NEXT.NEXT=Node[1]
It looks as expected, except for the last line. Node[0].next=Node[2]
is what I next to see. I do not see why Node[2].next=Node[1]
, but I believe the swap operation did not perform like I expected it to, and I am having difficulty understanding why.