Skip to content

Commit 6041a7d

Browse files
committed
leet code exercises
1 parent d0ccff1 commit 6041a7d

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

LeetCode/ArraysAndHashing/LongestConsecutiveSeq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ def longestConsecutive(self, nums: List[int]) -> int:
2828
counter +=1
2929
elif sortedNums[i] - sortedNums[i-1] <= 0:
3030
counter = counter
31-
else:
31+
elif sortedNums[i] - sortedNums[i-1] > 1:
3232
counters.append(counter)
33-
counter=0
33+
counter=1
3434

3535

3636
counters.append(counter)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Given the head of a singly linked list, reverse the list, and return the reversed list."""
2+
3+
from typing import Optional
4+
5+
# Definition for singly-linked list.
6+
class ListNode:
7+
def __init__(self, val=0, next=None):
8+
self.val = val
9+
self.next = next
10+
11+
class Solution:
12+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
13+
14+
current = head # intialize current pointer as head
15+
prev = None #initialize prev pointer as Null
16+
17+
while current: #loop till current points to null
18+
next= current.next #initialize next pointer as the next pointer of curr
19+
current.next = prev #assign prev pointer to curr's next pointer
20+
# assign current to prev and next to current
21+
prev = current
22+
current = next
23+
24+
return prev #return prev poiter to get reverse of the list
25+
26+
27+
28+

0 commit comments

Comments
 (0)