📅  最后修改于: 2020-10-15 04:49:29             🧑  作者: Mango
在此程序中,我们需要将单链列表的最后一个节点与第一个节点交换,以便第一个节点将成为最后一个节点,而最后一个节点将成为第一个节点。
考虑上面的例子;节点1代表列表的开头,节点4代表最后一个节点。为了将第一个节点与最后一个节点交换,我们将遍历列表,以使索引指向第二个最后一个节点,而current指向最后一个节点。节点温度将指向头。然后,将当前(最后一个节点)作为列表的新标题。现在,将整个列表移到旧的头之后,并将其附加到新的头之后。最后,在索引节点(倒数第二个节点)之后添加temp(旧头节点)。
#Represent a node of the singly linked list
class Node:
def __init__(self,data):
self.data = data;
self.next = None;
class Swap:
#Represent the head of the singly linked list
def __init__(self):
self.head = None;
#addNode() will add a new node to the list
def addNode(self, data):
#Create a new node
newNode = Node(data);
#Checks if the list is empty
if(self.head == None):
#If list is empty, head will point to new node
self.head = newNode;
else:
current = self.head;
while(current.next != None):
current = current.next;
#newNode will be added after last node of the list
current.next = newNode;
#swapLastWithFirst() will swap head node with the last node of the list
def swapLastWithFirst(self):
current = self.head;
temp = None;
index = None;
#If list is empty, then display the list as it is
if(self.head == None):
return;
else:
#After the loop, current will point to last element and index will point to second last element
while(current.next != None):
index = current;
current = current.next;
#If list contains only one node, then display list as it is
if(self.head == current):
return;
#If list contains only two nodes, then swap the head node with current node
elif(self.head.next == current):
temp = self.head;
#head will point to last node i.e. current
self.head = current;
#node next to new head will be the last node
self.head.next = temp;
#Node next to last element will be None
temp.next = None;
else:
temp = self.head;
#head will point to last node i.e. current
self.head = current;
#Detach the list from previous head and add it after new head
self.head.next = temp.next;
#Previous head will become last node of the list
index.next = temp;
#Node next to last element will be None
temp.next = None;
#display() will display all the nodes present in the list
def display(self):
#Node current will point to head
current = self.head;
if(self.head == None):
print("List is empty");
return;
while(current != None):
#Prints each node by incrementing pointer
print(current.data ),
current = current.next;
print "";
sList = Swap();
#Add nodes to the list
sList.addNode(1);
sList.addNode(2);
sList.addNode(3);
sList.addNode(4);
print("Originals list: ");
sList.display();
#Swaps Last node with first node
sList.swapLastWithFirst();
print("List after swapping the first node with last: ");
sList.display();
输出:
Originals list:
1 2 3 4
List after swapping the first node with last:
4 2 3 1
#include
//Represent a node of the singly linked list
struct node{
int data;
struct node *next;
};
//Represent the head of the singly linked list
struct node *head = NULL;
//addNode() will add a new node to the list
void addNode(int data) {
//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
//Checks if the list is empty
if(head == NULL) {
//If list is empty, head will point to new node
head = newNode;
}
else {
struct node *current = head;
while(current->next != NULL) {
current = current->next;
}
//newNode will be added after last node of the list
current->next = newNode;
}
}
//swapLastWithFirst() will swap head node with the last node of the list
void swapLastWithFirst() {
struct node *current = head, *temp = NULL, *index = NULL;
//If list is empty, then display the list as it is
if(head == NULL) {
return;
}
else {
//After the loop, current will point to last element and index will point to second last element
while(current->next != NULL) {
index = current;
current = current->next;
}
//If list contains only one node, then display list as it is
if(head == current) {
return;
}
//If list contains only two nodes, then swap the head node with current node
else if(head->next == current) {
temp = head;
//head will point to last node i.e. current
head = current;
//node next to new head will be the last node
head->next = temp;
//Node next to last element will be null
temp->next = NULL;
}
else {
temp = head;
//head will point to last node i.e. current
head = current;
//Detach the list from previous head and add it after new head
head->next = temp->next;
//Previous head will become last node of the list
index->next = temp;
//Node next to last element will be null
temp->next = NULL;
}
}
}
//display() will display all the nodes present in the list
void display() {
//Node current will point to head
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
while(current != NULL) {
//Prints each node by incrementing pointer
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
//Add nodes to the list
addNode(1);
addNode(2);
addNode(3);
addNode(4);
printf("Originals list: \n");
display();
//Swaps Last node with first node
swapLastWithFirst();
printf("List after swapping the first node with last: \n");
display();
return 0;
}
输出:
Originals list:
1 2 3 4
List after swapping the first node with last:
4 2 3 1
public class Swap {
//Represent a node of the singly linked list
class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
//Represent the head of the singly linked list
public Node head = null;
//addNode() will add a new node to the list
public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);
//Checks if the list is empty
if(head == null) {
//If list is empty, head will point to new node
head = newNode;
}
else {
Node current = head;
while(current.next != null) {
current = current.next;
}
//newNode will be added after last node of the list
current.next = newNode;
}
}
//swapLastWithFirst() will swap head node with the last node of the list
public void swapLastWithFirst() {
Node current = head, temp = null, index = null;
//If list is empty, then display the list as it is
if(head == null) {
return;
}
else {
//After the loop, current will point to last element and index will point to second last element
while(current.next != null) {
index = current;
current = current.next;
}
//If list contains only one node, then display list as it is
if(head == current) {
return;
}
//If list contains only two nodes, then swap the head node with current node
else if(head.next == current) {
temp = head;
//head will point to last node i.e. current
head = current;
//node next to new head will be the last node
head.next = temp;
//Node next to last element will be null
temp.next = null;
}
else {
temp = head;
//head will point to last node i.e. current
head = current;
//Detach the list from previous head and add it after new head
head.next = temp.next;
//Previous head will become last node of the list
index.next = temp;
//Node next to last element will be null
temp.next = null;
}
}
}
//display() will display all the nodes present in the list
public void display() {
//Node current will point to head
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {
//Prints each node by incrementing pointer
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
Swap sList = new Swap();
//Add nodes to the list
sList.addNode(1);
sList.addNode(2);
sList.addNode(3);
sList.addNode(4);
System.out.println("Originals list: ");
sList.display();
//Swaps Last node with first node
sList.swapLastWithFirst();
System.out.println("List after swapping the first node with last: ");
sList.display();
}
}
输出:
Originals list:
1 2 3 4
List after swapping the first node with last:
4 2 3 1
using System;
public class CreateList
{
//Represent a node of the singly linked list
public class Node{
public T data;
public Node next;
public Node(T value) {
data = value;
next = null;
}
}
public class Swap{
//Represent the head of the singly linked list
public Node head = null;
//addNode() will add a new node to the list
public void addNode(T data) {
//Create a new node
Node newNode = new Node(data);
//Checks if the list is empty
if(head == null) {
//If list is empty, head will point to new node
head = newNode;
}
else {
Node current = head;
while(current.next != null) {
current = current.next;
}
//newNode will be added after last node of the list
current.next = newNode;
}
}
//swapLastWithFirst() will swap head node with the last node of the list
public void swapLastWithFirst() {
Node current = head, temp = null, index = null;
//If list is empty, then display the list as it is
if(head == null) {
return;
}
else {
//After the loop, current will point to last element and index will point to second last element
while(current.next != null) {
index = current;
current = current.next;
}
//If list contains only one node, then display list as it is
if(head == current) {
return;
}
//If list contains only two nodes, then swap the head node with current node
else if(head.next == current) {
temp = head;
//head will point to last node i.e. current
head = current;
//node next to new head will be the last node
head.next = temp;
//Node next to last element will be null
temp.next = null;
}
else {
temp = head;
//head will point to last node i.e. current
head = current;
//Detach the list from previous head and add it after new head
head.next = temp.next;
//Previous head will become last node of the list
index.next = temp;
//Node next to last element will be null
temp.next = null;
}
}
}
//display() will display all the nodes present in the list
public void display() {
//Node current will point to head
Node current = head;
if(head == null) {
Console.WriteLine("List is empty");
return;
}
while(current != null) {
//Prints each node by incrementing pointer
Console.Write(current.data + " ");
current = current.next;
}
Console.WriteLine();
}
}
public static void Main()
{
Swap sList = new Swap();
//Add nodes to the list
sList.addNode(1);
sList.addNode(2);
sList.addNode(3);
sList.addNode(4);
Console.WriteLine("Originals list: ");
sList.display();
//Swaps Last node with first node
sList.swapLastWithFirst();
Console.WriteLine("List after swapping first node with last: ");
sList.display();
}
}
输出:
Originals list:
1 2 3 4
List after swapping first node with last:
4 2 3 1
data = $data;
$this->next = NULL;
}
}
class Swap{
//Represent the head of the singly linked list
public $head;
function __construct(){
$this->head = NULL;
}
//addNode() will add a new node to the list
function addNode($data) {
//Create a new node
$newNode = new Node($data);
//Checks if the list is empty
if($this->head == NULL) {
//If list is empty, head will point to new node
$this->head = $newNode;
}
else {
$current = $this->head;
while($current->next != null) {
$current = $current->next;
}
//newNode will be added after last node of the list
$current->next = $newNode;
}
}
//swapLastWithFirst() will swap head node with the last node of the list
function swapLastWithFirst() {
$current = $this->head;
$temp = null;
$index = null;
//If list is empty, then display the list as it is
if($this->head == null) {
return;
}
else {
//After the loop, current will point to last element and index will point to second last element
while($current->next != null) {
$index = $current;
$current = $current->next;
}
//If list contains only one node, then display list as it is
if($this->head == $current) {
return;
}
//If list contains only two nodes, then swap the head node with current node
else if($this->head->next == $current) {
$temp = $this->head;
//head will point to last node i.e. current
$this->head = $current;
//node next to new head will be the last node
$this->head->next = $temp;
//Node next to last element will be null
$temp->next = null;
}
else {
$temp = $this->head;
//head will point to last node i.e. current
$this->head = $current;
//Detach the list from previous head and add it after new head
$this->head->next = $temp->next;
//Previous head will become last node of the list
$index->next = $temp;
//Node next to last element will be null
$temp->next = null;
}
}
}
//display() will display all the nodes present in the list
function display() {
//Node current will point to head
$current = $this->head;
if($this->head == NULL) {
print("List is empty
");
return;
}
while($current != NULL) {
//Prints each node by incrementing pointer
print($current->data . " ");
$current = $current->next;
}
print("
");
}
}
$sList = new Swap();
//Add nodes to the list
$sList->addNode(1);
$sList->addNode(2);
$sList->addNode(3);
$sList->addNode(4);
print("Originals list:
");
$sList->display();
//Swaps Last node with first node
$sList->swapLastWithFirst();
print("List after swapping first node with last:
");
$sList->display();
?>
输出:
Originals list:
1 2 3 4
List after swapping first node with last:
4 2 3 1