Reputation: 13
I only have this part of the code. I'm unable to make sense is "next" another method of ptr or is it the random.next method. Just want to know if this is a common code fragment used by experts.
Node mystery(Node ptr) {
Node temp;
Node previous = null;
while (ptr != null) {
temp = ptr.next;
ptr.next = previous;
previous = ptr;
ptr = temp;
}
return previous;
}
Upvotes: 0
Views: 128
Reputation: 9506
You code reversing a single linked list
It's just another example of reversing a single linked list: (see this for more details)
// C# program for reversing the linked list
using System;
class GFG {
static void Main(string[] args)
{
LinkedList list = new LinkedList();
list.AddNode(new LinkedList.Node(85));
list.AddNode(new LinkedList.Node(15));
list.AddNode(new LinkedList.Node(4));
list.AddNode(new LinkedList.Node(20));
// List before reversal
Console.WriteLine("Given linked list:");
list.PrintList();
// Reverse the list
list.ReverseList();
// List after reversal
Console.WriteLine("Reversed linked list:");
list.PrintList();
}
}
class LinkedList {
Node head;
public class Node {
public int data;
public Node next;
public Node(int d)
{
data = d;
next = null;
}
}
// function to add a new node at
// the end of the list
public void AddNode(Node node)
{
if (head == null)
head = node;
else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
}
}
// function to reverse the list
public void ReverseList()
{
Node prev = null, current = head, next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}
// function to print the list data
public void PrintList()
{
Node current = head;
while (current != null) {
Console.Write(current.data + " ");
current = current.next;
}
Console.WriteLine();
}
}
Upvotes: 0
Reputation: 15257
You can execute the logic steps by steps, using your debugger or a piece of paper with a pen.
This looks like a linked list. Each node pointing to the next one, the last note pointing to null
head tail
+---+ +---+ +---+
|"A"| next |"B"| next |"C"| next
| |------>| |------>| |------> null
+---+ +---+ +---+
while (ptr != null) {
will loop until the current node is null
The code inside the while scope is a typical swap. Let's execute it steps by steps, considering ptr
is the node containing "A"
, like above
temp = ptr.next;
Now, temp
is poiting to the next node.
ptr
|
v
+---+ +---+ +---+
|"A"| next |"B"| next |"C"| next
| |------>| |------>| |------> null
+---+ +---+ +---+
^
|
temp
ptr.next = previous;
Now, ptr.next
is pointing to previous
which was initialized to null. ptr
is now de-linked from the list
ptr
|
v
+---+ | +---+ +---+
|"A"| next | |"B"| next |"C"| next
| |------> null | | |------>| |------> null
+---+ | +---+ +---+
| ^
| |
temp
previous = ptr;
Both ptr
and previous
are pointing to the same node
ptr
|
v
+---+ | +---+ +---+
|"A"| next | |"B"| next |"C"| next
| |------> null | | |------>| |------> null
+---+ | +---+ +---+
^ | ^
| | |
previous temp
ptr = temp;
Now, both ptr
and temp
are pointing to the same node
ptr
|
v
+---+ | +---+ +---+
|"A"| next | |"B"| next |"C"| next
| |------> null | | |------>| |------> null
+---+ | +---+ +---+
^ | ^
| | |
previous temp
Since ptr != null
is true, the loop continue :
temp = ptr.next;
ptr
|
v
+---+ | +---+ +---+
|"A"| next | |"B"| next |"C"| next
| |------> null | | |------>| |------> null
+---+ | +---+ +---+
^ | ^
| | |
previous temp
ptr.next = previous;
Now, the next item of the current node is the one saved previously (the "A"
node) :
ptr
|
v
+---+ +---+ | +---+
|"B"| next |"A"| next | |"C"| next
| |------> | |------> null | | |------> null
+---+ +---+ | +---+
^ | ^
| |
previous temp
previous = ptr;
ptr
|
v
+---+ +---+ | +---+
|"B"| next |"A"| next | |"C"| next
| |------> | |------> null | | |------> null
+---+ +---+ | +---+
^ | ^
| |
previous temp
ptr = temp;
ptr
|
v
+---+ +---+ | +---+
|"B"| next |"A"| next | |"C"| next
| |------> | |------> null | | |------> null
+---+ +---+ | +---+
^ | ^
| |
previous temp
Since ptr != null
is true, the loop continues and the same logic is applied :
temp = ptr.next;
ptr
|
v
+---+ +---+ | +---+
|"B"| next |"A"| next | |"C"| next
| |------> | |------> null | | |------> null
+---+ +---+ ^ | +---+ ^
^ | | |
| | |
previous +-----------------temp
ptr.next = previous;
ptr
|
v
+---+ +---+ +---+
|"C"| next |"B"| next |"A"| next
| |------> | |------> | |------> null
+---+ +---+ +---+ ^
^ |
| |
previous temp
previous = ptr;
ptr
|
v
+---+ +---+ +---+
|"C"| next |"B"| next |"A"| next
| |------> | |------> | |------> null
+---+ +---+ +---+ ^
^ |
| |
previous temp
ptr = temp;
ptr
|
+---+ +---+ +---+ |
|"C"| next |"B"| next |"A"| next v
| |------> | |------> | |------> null
+---+ +---+ +---+ ^
^ |
| |
previous temp
Now, while (ptr != null)
is false. The loop ends and previous
, which is the new head is returned.
This method reverse the order of a linked list and returns the head.
Upvotes: 1
Reputation: 2452
Usually Node object has a value (e.g of type int) and another property that holds the next node
public class Node{
public int Value {get; set;}
public Node Next {get; set;}
//...
}
For example here the next node of n1
is n2
node
Node n1=new Node(5);
Node n2=new Node(2);
n1.Next=n2;
Upvotes: 0