Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

reverse linked list with python

def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:

    previousNode, currentNode = None, head

    while currentNode:

        temp = currentNode.next
        currentNode.next = previousNode
        previousNode = currentNode
        currentNode = temp

    return previousNode
Comment

python reverse linked list


# Python program to reverse a linked list
# Time Complexity : O(n)
# Space Complexity : O(n) as 'next'
#variable is getting created in each loop.
 
# Node class
 
 
class Node:
 
    # Constructor to initialize the node object
    def __init__(self, data):
        self.data = data
        self.next = None
 
 
class LinkedList:
 
    # Function to initialize head
    def __init__(self):
        self.head = None
 
    # Function to reverse the linked list
    def reverse(self):
        prev = None
        current = self.head
        while(current is not None):
            next = current.next
            current.next = prev
            prev = current
            current = next
        self.head = prev
 
    # Function to insert a new node at the beginning
    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node
 
    # Utility function to print the LinkedList
    def printList(self):
        temp = self.head
        while(temp):
            print (temp.data,end=" ")
            temp = temp.next
 
 
# Driver program to test above functions
llist = LinkedList()
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)
 
print ("Given Linked List")
llist.printList()
llist.reverse()
print ("
Reversed Linked List")
llist.printList()
Comment

reverse linked list

#include <iostream>
using namespace std;

typedef struct node Node;

struct node{
    
    int data;
    Node *next;
};

Node *Create_Node(int data, Node *next){
    
    Node *new_node = (Node *) malloc(sizeof(Node));
    new_node->data = data;
    new_node->next = next;
    return new_node;
}

Node *Insert_Node(Node *head, int data){
    
    Node *new_node = Create_Node(data, NULL);
    
    if(head == NULL){
        return new_node;
    }
    Node *current_node = head;
    while(current_node->next != NULL){
        
        current_node = current_node->next;
    }
    
    current_node->next = new_node;
    
    return head;
}

Node *Reverse_LinkedList(Node *head){
    
    Node *temp = NULL;
    Node *prev_data = NULL;
    Node *current_node = head;
    
    while(current_node != NULL){
        
        prev_data = current_node;
        current_node = current_node->next;
        prev_data->next = temp;
        temp = prev_data;
    }
    head = prev_data;
    return head;
}

void Print_LinkedList(Node *head){
    
    Node *current_node = head;
    while(current_node != NULL){
        printf("%d ",current_node->data);
        current_node = current_node->next;
    }
    printf("
");
}

int main() {
	
	Node *head;
	
	head = Insert_Node(head, 10);
	head = Insert_Node(head, 20);
	head = Insert_Node(head, 30);
	
	Print_LinkedList(head);
		
	head = Reverse_LinkedList(head);
	
	Print_LinkedList(head);
	return 0;
}
Comment

Reverse Linked List

 public ListNode ReverseList(ListNode head) 
 {
   if(head==null || head.next == null)
   	return head;

  var t = ReverseList(head.next);
  head.next.next = head;
  head.next = null;

  return t;
}
Comment

reverse linked list

def reverseLinkedList(self):
        # Complexity Time: O(n), Complexity Memory: O(1)
        # Initialize previous and current pointers
        prev, curr = None, self.head
        # loop over the linked list nodes until the current is Null 
        while curr:
        	# Store current's next node in a temporary variable 
            next = curr.next
            # Reverse current node's pointer
            curr.next = prev
            # Move pointers one position ahead
            prev = curr
            curr = next
        self.head = prev
Comment

Reverse a Linked List

	    /* Before changing next pointer of current node,
        store the next node */
        next = curr -> next
        /*  Change next pointer of current node */
        /* Actual reversing */
        curr -> next = prev
        /*  Move prev and curr one step ahead */
        prev = curr
        curr = next
Comment

reverse a linked list

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        
        ListNode current = head;
        ListNode prev = null;
        ListNode next = null;
        
        if(head == null)
        {
            return head;
        }
        while(current != null)
        {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        return prev;
    }
}
Comment

Reverse a Linked List

void reverse()
	{
		node* current, * prev, * temp;
		current = head;
		prev = NULL;
		while (current != NULL)
		{
			temp = current->next;
			current->next = prev;
			prev = current;
			current = temp;
		}
		head = prev;
	}
Comment

reverse linked list python

# Python program to reverse a linked list
# Time Complexity : O(n)
# Space Complexity : O(n) as 'next' 
#variable is getting created in each loop.
  
# Node class
  
  
class Node:
  
    # Constructor to initialize the node object
    def __init__(self, data):
        self.data = data
        self.next = None
  
  
class LinkedList:
  
    # Function to initialize head
    def __init__(self):
        self.head = None
  
    # Function to reverse the linked list
    def reverse(self):
        prev = None
        current = self.head
        while(current is not None):
            next = current.next
            current.next = prev
            prev = current
            current = next
        self.head = prev
  
    # Function to insert a new node at the beginning
    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node
  
    # Utility function to print the LinkedList
    def printList(self):
        temp = self.head
        while(temp):
            print (temp.data,end=" ")
            temp = temp.next
  
  
# Driver program to test above functions
llist = LinkedList()
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)
  
print ("Given Linked List")
llist.printList()
llist.reverse()
print ("
Reversed Linked List")
llist.printList()
  
Comment

PREVIOUS NEXT
Code Example
Python :: three different randomn numbers python 
Python :: how to convert .py into .exe through pytohn scripts 
Python :: python dataframe limit rows 
Python :: editing specific line in text file in python 
Python :: to text pandas 
Python :: comparing values in python 
Python :: optimize python code 
Python :: how to remove .0 from string column with empty strings in python 
Python :: python webscraper stack overflow 
Python :: pandas mask multiple condition 
Python :: python loop function 
Python :: get the largest of 2 strings python 
Python :: python stack size 
Python :: import turtle t=turtle.turtle() def star(t): for t in range(5): t.color("red") t.pendown() t.begin_fill() t.forward(100) t.right(144) t.end_fill() 
Python :: stackoverflow - import data on colabs 
Python :: john cabot 
Python :: santhal paragana 
Python :: without @tf.function OOM 
Python :: roll a dice 
Python :: how to update pip python 
Shell :: ubuntu restart sound 
Shell :: install sklearn 
Shell :: docker remove none images 
Shell :: git set email for project 
Shell :: bash: gedit: command not found 
Shell :: remove remote origin 
Shell :: flush dns cmd 
Shell :: how to convert ui to py pyqt5 
Shell :: git reset all submodules 
Shell :: install gem ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =