-
Notifications
You must be signed in to change notification settings - Fork 0
/
92. Reverse Linked List II.py
115 lines (102 loc) · 2.61 KB
/
92. Reverse Linked List II.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class ListNode:
def __init__(self,initdata):
self.val = initdata
self.next = None
def getData(self):
return self.val
def getNext(self):
return self.next
def setData(self,newdata):
self.val = newdata
def setNext(self,newnext):
self.next = newnext
def tolist(self):
p = self
re = []
while p!=None:
re.append(p.val)
p=p.next
return re
class OrderedList:
def __init__(self):
self.head = None
def search(self,item):
current = self.head
found = False
stop = False
while current != None and not found and not stop:
if current.getData() == item:
found = True
else:
if current.getData() > item:
stop = True
else:
current = current.getNext()
return found
def add(self,li):
if isinstance(li,int):
li = [li]
for item in li:
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.getData() > item:
stop = True
else:
previous = current
current = current.getNext()
temp = ListNode(item)
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.getNext()
return count
def tolist(self):
p = self.head
re = []
while p!=None:
re.append(p.val)
p=p.next
return re
l = OrderedList()
l.add([1,2,3,4,5,6])
h = l.head
class Solution(object):
def reverseBetween(self, head, m, n):
"""
:type head: ListNode
:type m: int
:type n: int
:rtype: ListNode
"""
pre= ListNode(0)
p = pre
pre.next = head
c = 1
while c<m:
pre = head
head = head.next
c += 1
p1 = pre
p2 = head
pre = None
while c>=m and c<=n:
curr = head.next
head.next = pre
pre = head
head = curr
c+=1
p1.next = pre
p2.next = curr
return p.next
Solution().reverseBetween(h,1,3)
print l.tolist()