给定指向链表头节点的指针,任务是反转链表。我们需要通过更改节点之间的链接来反转列表。
例如:
输入:以下链表
的头部 1->2->3->4->NULL 输出:链表应更改为 4
->3->2->1->NULL
输入:以下链表
的头部 1->2->3->4->5->NULL 输出:链表应更改为
5->4->3->2->1->NULL
输入:空 输出:空
输入:1->空
输出:1->空
思路1:
时间复杂度:O(N),遍历大小为 N 的链表。
辅助空间:O(1)
// Java program for reversing the linked list
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
/* Function to reverse the linked list */
Node reverse(Node node)
{
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}
// prints content of double linked list
void printList(Node node)
{
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
// Driver Code
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node(85);
list.head.next = new Node(15);
list.head.next.next = new Node(4);
list.head.next.next.next = new Node(20);
System.out.println("Given linked list");
list.printList(head);
head = list.reverse(head);
System.out.println("");
System.out.println("Reversed linked list ");
list.printList(head);
}
}
// This code has been contributed by Mayank Jaiswal
思路2:使用堆栈反转链表:
这个想法是存储堆栈中的所有节点,然后制作一个反向链表。
请按照以下步骤解决问题:
- 将节点(值和地址)存储在堆栈中,直到输入所有值。
- 完成所有输入后,将 Head 指针更新到最后一个位置(即最后一个值)。
- 开始弹出节点(值和地址)并以相同的顺序存储它们,直到堆栈为空。
- 将堆栈中最后一个节点的下一个指针更新为 NULL。
-
// Java program for above approach import java.util.*; class GFG { // Create a class Node to enter values and address in // the list static class Node { int data; Node next; }; static Node head = null; // Function to reverse the linked list static void reverseLL() { // Create a stack "s" of Node type Stack<Node> s = new Stack<>(); Node temp = head; while (temp.next != null) { // Push all the nodes in to stack s.add(temp); temp = temp.next; } head = temp; while (!s.isEmpty()) { // Store the top value of stack in list temp.next = s.peek(); // Pop the value from stack s.pop(); // update the next pointer in the list temp = temp.next; } temp.next = null; } // Function to Display the elements in List static void printlist(Node temp) { while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } } // Program to insert back of the linked list static void insert_back(int value) { // we have used insertion at back method to enter // values in the list.(eg: head.1.2.3.4.Null) Node temp = new Node(); temp.data = value; temp.next = null; // If *head equals to null if (head == null) { head = temp; return; } else { Node last_node = head; while (last_node.next != null) last_node = last_node.next; last_node.next = temp; return; } } // Driver Code public static void main(String[] args) { insert_back(1); insert_back(2); insert_back(3); insert_back(4); System.out.print("Given linked list\n"); printlist(head); reverseLL(); System.out.print("\nReversed linked list\n"); printlist(head); } } // This code is contributed by Aditya Kumar (adityakumar129)
时间复杂度:O(N),访问大小为 N 的链表的每个节点.
辅助空间:O(N),空格用于存储堆栈中的节点。