Switching the links in a linked list

  • Context: Python 
  • Thread starter Thread starter FallenApple
  • Start date Start date
  • Tags Tags
    Links List
Click For Summary

Discussion Overview

The discussion revolves around the manipulation of pointers in a linked list to achieve a specific reordering of nodes. Participants explore systematic approaches to interchange links between nodes, addressing challenges encountered during the process.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes an attempt to rearrange a linked list from a->b->c->d to b->a->d->c by manipulating pointers, but encounters unexpected results.
  • Another participant suggests avoiding the use of changed links and emphasizes careful ordering of operations or using temporary pointers to prevent conflicts during swaps.
  • A different participant proposes implementing the linked list as a doubly linked list to facilitate pointer management, warning about potential memory leaks from improper pointer manipulation.
  • Another participant advises checking for null pointers to ensure that nodes are valid before performing operations.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to manipulate linked lists, with no consensus on a single method or solution. The discussion remains unresolved regarding the optimal strategy for pointer manipulation.

Contextual Notes

Participants highlight the importance of pointer integrity and the potential for errors when links are altered. There are mentions of specific cases, such as adjacent versus non-adjacent node swaps, which may complicate the pointer manipulation process.

FallenApple
Messages
564
Reaction score
61
So I have a linked list a->b->c->d

and I want to make another list

b->a->d->c

in a systematic way by interchanging pointers. But for some reason it isn't working. All I did was move the link between b and c into b to a. Then the link from a to b, changed to a to d. Then the one connecting c to d is reversed. I'm not sure what went wrong.
Python:
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None
       
       
       
A=['a','b','c','d']
head=ListNode(A[0])
#filling the linked list with abcd
temp=head
for i in A[1:]:
  temp.next=ListNode(i)
  temp=temp.next
 
 
point=head
print("the current linked list")

while(point):
  print(point.val)
  point=point.next

print("   ")

#attempting to alter the list
Temp1=head.next.next  
head.next.next=head    # moving the link between b-c to a
Temp2=head.next
head.next=head.next.next.next  #moving the link between a-b to d
head.next.next.next=Temp1    #moving the link between d and c
head=Temp2

#print(head)
print("the new linkedlist")
point=head

while(point):
  print(point.val)
  point=point.next
The output is

the current linked list
a
b
c
d

the new linkedlist
b
a
c
d
 
Technology news on Phys.org
You have to avoid using any links that you've changed, either with careful ordering of operations and/or using temp pointers to avoid conflicts.

For example, if swapping two nodes in a linked list, if the nodes are adjacent, the next pointers are effectively rotated, but if the nodes are not adjacent, then two pairs of next pointers are swapped. This can be handled with common code by first swapping the external pointers to the two nodes, followed by swapping the nodes internal next pointers.

Given a list a->b->c->d->e->f, to swap b and e, swap(a->next, d->next), swap(b->next, e->next). To swap c and d, swap(b->next, c->next), swap(c->next, d->next).
 
  • Like
Likes   Reactions: FallenApple
Implement as a doubly linked list. In that data structure any given node knows who's it is attached to. Be very careful with the pointer manipulation or orphaned nodes (and thus memory leaks) will occur
 
  • Like
Likes   Reactions: FallenApple
Also make sure to test the link pointers for null so that the node is valid. Usually the "next" pointer is null to indicate there are no further nodes
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 20 ·
Replies
20
Views
6K
Replies
15
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K