📅  最后修改于: 2023-12-03 15:26:05.278000             🧑  作者: Mango
The UGC-NET CS 2017 November - III Question 22 is a question related to computer science and programming. This question requires the programmer to write code to perform certain tasks. The tasks include implementing a linked list, traversing the linked list, and reversing the linked list. The goal of this question is to assess the programming skills and knowledge of the programmer.
## Linked List Implementation and Traversal
```python
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
current_node = self.head
while current_node.next is not None:
current_node = current_node.next
current_node.next = new_node
def traverse(self):
current_node = self.head
while current_node is not None:
print(current_node.data, end=" ")
current_node = current_node.next
```markdown
## Reverse Linked List
```python
def reverse_list(llist):
prev_node = None
current_node = llist.head
while current_node is not None:
next_node = current_node.next
current_node.next = prev_node
prev_node = current_node
current_node = next_node
llist.head = prev_node
```markdown
The program consists of two parts: linked list implementation and traversal and reverse linked list.
The first part defines two classes, Node and LinkedList. Node is a class that represents a node in a linked list. LinkedList is a class that represents a linked list. The LinkedList class contains an insert method that adds a new node to the end of the linked list, and a traverse method that prints the data of each node in the linked list.
The second part defines a reverse_list function that takes a LinkedList object as input and reverses the order of its nodes. This function uses three variables: prev_node, current_node, and next_node. prev_node refers to the node that comes before the current_node, current_node refers to the node that is being processed, and next_node refers to the node that comes after the current_node. The function uses a while loop to iterate over the nodes in the linked list, and uses the current_node.next, current_node, and prev_node variables to reverse the links between nodes.
Overall, this program demonstrates the programmer's ability to work with linked lists and manipulate them in a variety of ways.