Skip to content

Commit 8eb0c20

Browse files
authored
Merge pull request #830 from dsrao711/issue_740
Issue 740
2 parents 0b65a9e + 3b7acac commit 8eb0c20

File tree

3 files changed

+57
-48
lines changed

3 files changed

+57
-48
lines changed

DSA 450 GFG/next_greater_element.py

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#https://leetcode.com/problems/reverse-linked-list/
2+
3+
# Iterative method
4+
#Approach :
5+
6+
# Store the head in a temp variable called current .
7+
8+
# curr = head , prev = null
9+
10+
# Now for a normal linked list , the current will point to the next node and so on till null
11+
# For reverse linked list, the current node should point to the previous node and the first node here will point to null
12+
13+
# Keep iterating the linkedlist until the last node and keep changing the next of the current node to prev node and also
14+
# update the prev node to current node and current node to next node
15+
16+
17+
# class ListNode:
18+
# def __init__(self, val=0, next=None):
19+
# self.val = val
20+
# self.next = next
21+
22+
class Solution:
23+
24+
def reverseList(self, head):
25+
curr = head
26+
prev = None
27+
while(curr != None):
28+
next = curr.next
29+
curr.next = prev
30+
prev = curr
31+
curr = next
32+
33+
return prev
34+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Approach :
2+
# Divide the linked list to two halved
3+
# First half is head and the remaining as rest
4+
# The head points to the rest in a normal linked list
5+
# In the reverse linked list , the next of current points to the prev node and the head node should point to NULL
6+
# Keep continuing this process till the last node
7+
8+
9+
# Definition for singly-linked list.
10+
# class ListNode:
11+
# def __init__(self, val=0, next=None):
12+
# self.val = val
13+
# self.next = next
14+
15+
16+
class Solution:
17+
def reverseList(self, head):
18+
if head is None or head.next is None:
19+
return head
20+
rest = self.reverseList(head.next)
21+
head.next.next = head
22+
head.next = None
23+
return rest

0 commit comments

Comments
 (0)