class Stack: singlyLinkedList = None def __init__(self): self.singlyLinkedList = SinglyLinkedList() def push(self, value): self.singlyLinkedList.insertIndex(1, value) def pop(self): self.singlyLinkedList.deleteIndex(1)
def test_list_init(self): """ Test new SinglyLinkedList initialization """ linkedList = SinglyLinkedList() self.assertIsInstance(linkedList, SinglyLinkedList) self.assertEqual(linkedList.length, 0) self.assertIsNone(linkedList.head) self.assertIsNone(linkedList.tail)
def test_shift(self): """ Tests the SinglyListedList.shift() method """ linkedList = SinglyLinkedList() linkedList.push(0) linkedList.push(1) linkedList.push(2) # shift node with data 0 from [0, 1, 2] shifted = linkedList.shift() self.assertIsInstance(shifted, Node) self.assertIsNone(shifted.next) self.assertEqual(shifted.data, 0) self.assertEqual(linkedList.head.data, 1) self.assertEqual(linkedList.tail.data, 2) self.assertEqual(linkedList.length, 2) # shift node with data 1 from [1, 2] shifted = linkedList.shift() self.assertIsInstance(shifted, Node) self.assertIsNone(shifted.next) self.assertEqual(shifted.data, 1) self.assertEqual(linkedList.head.data, 2) self.assertIsNone(linkedList.head.next) self.assertEqual(linkedList.tail.data, 2) self.assertIsNone(linkedList.tail.next) self.assertEqual(linkedList.length, 1) # shift node with data 2 from [2] shifted = linkedList.shift() self.assertIsInstance(shifted, Node) self.assertIsNone(shifted.next) self.assertEqual(shifted.data, 2) self.assertIsNone(linkedList.head) self.assertIsNone(linkedList.tail) self.assertEqual(linkedList.length, 0) # shift empty list shifted = linkedList.shift() self.assertIsNone(shifted) self.assertIsNone(linkedList.head) self.assertIsNone(linkedList.tail)
def test_pop(self): """ Tests the SinglyListedList.pop() method """ linkedList = SinglyLinkedList() linkedList.push('node zero') linkedList.push('node one') linkedList.push('node two') self.assertEqual(linkedList.length, 3) popped = linkedList.pop() self.assertEqual(popped.data, 'node two') self.assertEqual(linkedList.length, 2) popped = linkedList.pop() self.assertEqual(popped.data, 'node one') self.assertEqual(linkedList.length, 1) popped = linkedList.pop() self.assertEqual(popped.data, 'node zero') self.assertEqual(linkedList.length, 0) popped = linkedList.pop() self.assertIsNone(popped) self.assertEqual(linkedList.length, 0)
def test_unshift(self): """ Tests the SinglyListedList.unshift(data) method """ linkedList = SinglyLinkedList() linkedList.unshift('node last') self.assertEqual(linkedList.length, 1) self.assertEqual(linkedList.head.data, 'node last') self.assertEqual(linkedList.tail.data, 'node last') linkedList.unshift('node middle') self.assertEqual(linkedList.length, 2) self.assertEqual(linkedList.head.data, 'node middle') self.assertEqual(linkedList.tail.data, 'node last') linkedList.unshift('node first') self.assertEqual(linkedList.length, 3) self.assertEqual(linkedList.head.data, 'node first') self.assertEqual(linkedList.tail.data, 'node last')
def test_insert(self): """ Tests the SinglyListedList.insert(index, data) method successful insert returns true, else false """ linkedList = SinglyLinkedList() # can insert index 0 of empty list self.assertFalse(linkedList.insert(1, 'false')) self.assertTrue(linkedList.insert(0, 'true')) # can insert at tail self.assertTrue(linkedList.insert(1, 'true')) # can not insert any index out of bounds self.assertFalse(linkedList.insert(999, 'node out of bounds')) self.assertFalse(linkedList.insert(-1, 'node out of bounds')) # can insert into middle of list # current list = ['true', 'true'] self.assertTrue(linkedList.insert(1, 'middle node')) self.assertEqual(linkedList.to_list(), ['true', 'middle node', 'true'])
class Queue: singlyLinkedList = None def __init__(self): self.singlyLinkedList = SinglyLinkedList() def enqueue(self, value): self.singlyLinkedList.insertIndex(1, value) def dequeue(self): self.singlyLinkedList.deleteIndex(self.singlyLinkedList.size) def printQ(self): self.singlyLinkedList.printList() print()
def test_push(self): """ Tests the SinglyLinkedList.push(data) method """ linkedList = SinglyLinkedList() # push first Node linkedList.push('node zero') self.assertEqual(linkedList.length, 1) self.assertIsInstance(linkedList.head, Node) self.assertIsInstance(linkedList.tail, Node) # push second Node linkedList.push('node one') self.assertEqual(linkedList.length, 2) self.assertEqual(linkedList.head.data, 'node zero') self.assertEqual(linkedList.tail.data, 'node one') self.assertEqual(linkedList.head.next.data, 'node one')
def getOddEvenList(head): if not head: return head cur = head pos = 0 evens = [] odds = [] while cur: if pos % 2 == 0: evens.append(cur.value) else: odds.append(cur.value) pos += 1 cur = cur.next LL = SinglyLinkedList() while evens: LL.addAtTail(evens[0]) evens.pop(0) while odds: LL.addAtTail(odds[0]) odds.pop(0) return LL.head
def __init__(self): ''' 初始化。 ''' self.__cache = SinglyLinkedList() self.__maxCacheSize = 5
if size - n == 0: return head.next i = 0 node = head prev = head while node is not None: node = node.next i += 1 if i == size - n: prev.next = node.next return head prev = prev.next if __name__ == "__main__": LL = SinglyLinkedList() LL.addAtHead(1) LL.addAtTail(2) LL.addAtTail(3) LL.addAtTail(4) LL.addAtTail(5) print("Before -> ", end="") print(LL) head = removeNthNodeFromEnd(LL.head, 2) print("After -> ", end="") printList(head) LL1 = SinglyLinkedList() LL1.addAtHead(1) print("Before -> ", end="") print(LL1)
The number of the nodes in the list is in the range [0, 104]. -105 <= Node.val <= 105 pos is -1 or a valid index in the linked-list. Follow up: Can you solve it using O(1) (i.e. constant) memory? """ from singlyLinkedList import SinglyLinkedList def hasCycle(head): if head: fast = head.next slow = head while fast and fast.next: if fast.value == slow.value: return True fast = fast.next.next slow = slow.next return False if __name__ == "__main__": # Your LinkedList object will be instantiated and called as such: LL = SinglyLinkedList() LL.addAtHead(0) LL.addAtIndex(0, 2) LL.addAtIndex(0, 3) LL.addAtTail(-4) LL.addAtIndex(1, 30) print(LL) print(hasCycle(LL.head))
else: #print(nodeA.val) nodeA = nodeA.next if nodeB is None: nodeB = headA else: #print(nodeB.val) nodeB = nodeB.next return nodeA.value if __name__ == "__main__": intersect_value = 8 intersect_in_LL_A = 2 LL_A = SinglyLinkedList() LL_A.addAtHead(4) LL_A.addAtTail(1) LL_A.addAtTail(8) LL_A.addAtTail(4) LL_A.addAtTail(5) print(LL_A) LL_B = SinglyLinkedList() LL_B.addAtHead(5) LL_B.addAtTail(6) LL_B.addAtTail(1) LL_B.get(2).next = LL_A.get(intersect_in_LL_A) print(LL_B) print(getIntersectionNode(LL_A.head, LL_B.head))
""" from singlyLinkedList import SinglyLinkedList from usefulLinkedListMethods import printList def removeNodes(head, value): if not head: return head while head and head.value == value: head = head.next cur = head while cur and cur.next: if cur.next.value == value: cur.next = cur.next.next else: cur = cur.next return head if __name__ == "__main__": LL = SinglyLinkedList() LL.addAtHead(1) LL.addAtTail(6) LL.addAtTail(3) LL.addAtTail(4) LL.addAtTail(5) LL.addAtTail(6) value = 6 head = removeNodes(LL.head, value) printList(head)
class SinglyLinkedListTest(unittest.TestCase): def setUp(self): self.linkedList = SinglyLinkedList() def test_addFirst(self): self.linkedList.addFirst('first') self.linkedList.addFirst('second') self.assertEqual(self.linkedList.toString(), 'second,first') def test_addLast(self): self.linkedList.addLast('first') self.linkedList.addLast('second') self.assertEqual(self.linkedList.toString(), 'first,second') def test_addFront(self): self.linkedList.addFront(2, 'end') self.linkedList.addFront(0, 'first') self.linkedList.addFront(2, 'end2') self.linkedList.addFront(1, 'second') self.linkedList.addFront(2, 'third') self.linkedList.addFront(1, 'second1') self.assertEqual(self.linkedList.toString(), 'first,second1,second,third,end,end2') def test_addBack(self): self.linkedList.addBack(2, 'end') self.linkedList.addBack(0, 'first') self.linkedList.addBack(2, 'end2') self.linkedList.addBack(1, 'second') self.linkedList.addBack(2, 'third') self.linkedList.addBack(1, 'second1') self.assertEqual(self.linkedList.toString(), 'end,first,second1,second,third,end2') def test_removeFirst(self): self.linkedList.addFirst('first') self.linkedList.addFirst('second') self.assertEqual(self.linkedList.removeFirst(), 'second') self.assertEqual(self.linkedList.toString(), 'first') self.assertEqual(self.linkedList.removeFirst(), 'first') self.assertIsNone(self.linkedList.removeFirst()) def test_remove(self): self.linkedList.addFirst('third') self.linkedList.addFirst('second') self.linkedList.addFirst('first') self.assertIsNone(self.linkedList.remove(3)) self.assertEqual(self.linkedList.remove(0), 'first') self.assertEqual(self.linkedList.remove(1), 'third') self.assertEqual(self.linkedList.toString(), 'second') def test_removeLast(self): self.assertIsNone(self.linkedList.removeLast()) self.linkedList.addFirst('third') self.linkedList.addFirst('second') self.linkedList.addFirst('first') self.assertEqual(self.linkedList.removeLast(), 'third') self.assertEqual(self.linkedList.removeLast(), 'second') self.assertEqual(self.linkedList.removeLast(), 'first') self.assertIsNone(self.linkedList.removeLast()) def test_size(self): self.assertEqual(self.linkedList.size(), 0) self.linkedList.addFirst('third') self.linkedList.addFirst('second') self.linkedList.addFirst('first') self.assertEqual(self.linkedList.size(), 3) def test_empty(self): self.assertTrue(self.linkedList.empty()) self.linkedList.addFirst('third') self.linkedList.addFirst('second') self.linkedList.addFirst('first') self.assertFalse(self.linkedList.empty()) def test_get(self): self.assertIsNone(self.linkedList.get(1)) self.linkedList.addFirst('third') self.linkedList.addFirst('second') self.linkedList.addFirst('first') self.assertEqual(self.linkedList.get(1), 'second') self.assertIsNone(self.linkedList.get(3)) def test_indexOf(self): self.assertEqual(self.linkedList.indexOf('test'), -1) self.linkedList.addFirst('third') self.linkedList.addFirst('second') self.linkedList.addFirst('first') self.assertEqual(self.linkedList.indexOf('first'), 0) self.assertEqual(self.linkedList.indexOf('second'), 1) self.assertEqual(self.linkedList.indexOf('third'), 2) self.assertEqual(self.linkedList.indexOf('thirdt'), -1)
def test_get(self): """ Tests the SinglyListedList.get(index) method """ linkedList = SinglyLinkedList() linkedList.push(0) linkedList.push(1) linkedList.push(2) # check index of out bounds returns None self.assertIsNone(linkedList.get(-1)) self.assertIsNone(linkedList.get(3)) # test indices self.assertEqual(linkedList.get(0).data, 0) self.assertEqual(linkedList.get(1).data, 1) self.assertEqual(linkedList.get(2).data, 2) # last self.assertEqual(linkedList.get(linkedList.length - 1).data, 2)
def __init__(self): self.singlyLinkedList = SinglyLinkedList()
class SinglyLinkedListTest(unittest.TestCase): def setUp(self): self.list = SinglyLinkedList() def test_node(self): a = Node(2) b = Node(3) a.next = b self.assertEqual(2, a.data) self.assertEqual(3, a.next.data) self.assertEqual(3, b.data) self.assertIsNone(b.next) def test_addFirst(self): self.list.addFirst(1) self.list.addFirst(2) self.list.addFirst(3) self.list.addFirst(4) self.assertEqual('4,3,2,1', self.list.toString()) def test_addLast(self): self.list.addLast(1) self.list.addLast(2) self.list.addLast(3) self.list.addLast(4) self.assertEqual('1,2,3,4', self.list.toString()) def test_addFront(self): self.list.addFront(1, 0) self.list.addFront(2, 1) self.list.addFront(3, 1) self.list.addFront(4, 2) self.assertEqual('1,3,4,2', self.list.toString()) def test_addBack(self): self.list.addBack(1, 0) self.list.addBack(2, 1) self.list.addBack(3, 1) self.list.addBack(4, 1) self.assertEqual('1,2,4,3', self.list.toString()) def _addNodes(self): self.list.addLast(1) self.list.addLast(2) self.list.addLast(3) self.list.addLast(4) self.list.addLast(5) def test_removeFirst(self): self._addNodes() self.assertEqual(1, self.list.removeFirst()) self.assertEqual(2, self.list.removeFirst()) self.assertEqual(3, self.list.removeFirst()) self.assertEqual(4, self.list.removeFirst()) self.assertEqual(5, self.list.removeFirst()) self.assertIsNone(self.list.removeFirst()) def test_remove(self): self._addNodes() self.assertIsNone(self.list.remove(5)) self.assertEqual(5, self.list.remove(4)) self.assertEqual(1, self.list.remove(0)) self.assertEqual(3, self.list.remove(1)) self.assertEqual(4, self.list.remove(1)) self.assertEqual(2, self.list.remove(0)) def test_removeLast(self): self._addNodes() self.assertEqual(5, self.list.removeLast()) self.assertEqual(4, self.list.removeLast()) self.assertEqual(3, self.list.removeLast()) self.assertEqual(2, self.list.removeLast()) self.assertEqual(1, self.list.removeLast()) self.assertIsNone(self.list.removeLast()) def test_size(self): self.assertEqual(0, self.list.size()) self._addNodes() self.assertEqual(5, self.list.size()) def test_empty(self): self.assertEqual(True, self.list.empty()) self._addNodes() self.assertEqual(False, self.list.empty()) def test_get(self): self._addNodes() self.assertIsNone(self.list.get(-1)) self.assertEqual(1, self.list.get(0)) self.assertEqual(2, self.list.get(1)) self.assertEqual(3, self.list.get(2)) self.assertEqual(4, self.list.get(3)) self.assertEqual(5, self.list.get(4)) self.assertIsNone(self.list.get(5)) def test_indexOf(self): self._addNodes() self.assertEqual(0, self.list.indexOf(1)) self.assertEqual(1, self.list.indexOf(2)) self.assertEqual(2, self.list.indexOf(3)) self.assertEqual(3, self.list.indexOf(4)) self.assertEqual(4, self.list.indexOf(5)) self.assertEqual(-1, self.list.indexOf(6))
return head if head.value == value: head = head.next return head cur = head while cur and cur.next: if cur.next.value == value: cur.next = cur.next.next else: cur = cur.next return head if __name__ == "__main__": LL = SinglyLinkedList() LL.addAtHead(4) LL.addAtTail(5) LL.addAtTail(1) LL.addAtTail(9) value = 5 head = deleteNode(LL.head, value) printList(head) LL = SinglyLinkedList() LL.addAtHead(4) LL.addAtTail(5) LL.addAtTail(1) LL.addAtTail(9) value = 1 head = deleteNode(LL.head, value)
def test_clear(self): """ Tests the SinglyListedList.reverse() method """ linkedList = SinglyLinkedList() linkedList.push(0) linkedList.push(1) linkedList.push(2) linkedList.push(3) linkedList.push(4) linkedList.push(5) self.assertEqual(linkedList.to_list(), [0, 1, 2, 3, 4, 5]) self.assertIsInstance(linkedList.clear(), SinglyLinkedList) self.assertEqual(linkedList.to_list(), [])
def test_reverse(self): """ Tests the SinglyListedList.reverse() method """ linkedList = SinglyLinkedList() linkedList.push(0) linkedList.push(1) self.assertEqual(linkedList.to_list(), [0, 1]) linkedList.reverse() self.assertEqual(linkedList.to_list(), [1, 0]) linkedList.reverse() # back to [0, 1] linkedList.push(2) # now [0, 1, 2] self.assertEqual(linkedList.to_list(), [0, 1, 2]) linkedList.reverse() self.assertEqual(linkedList.to_list(), [2, 1, 0]) linkedList = SinglyLinkedList() linkedList.push(0) linkedList.push(1) linkedList.push(2) linkedList.push(3) linkedList.push(4) linkedList.push(5) self.assertEqual(linkedList.to_list(), [0, 1, 2, 3, 4, 5]) linkedList.reverse() self.assertEqual(linkedList.to_list(), [5, 4, 3, 2, 1, 0])
def test_delete(self): """ Tests the SinglyListedList.delete(index) method successful delete returns true, else false """ linkedList = SinglyLinkedList() # can not delete from empty list self.assertFalse(linkedList.delete(0)) # can delete from list with single item linkedList.push(1) self.assertEqual(linkedList.to_list(), [1]) self.assertTrue(linkedList.delete(0)) self.assertEqual(linkedList.to_list(), []) linkedList.push(1) linkedList.push(2) linkedList.push(3) # can not delete index out of bounds self.assertEqual(False, linkedList.delete(-1)) self.assertEqual(False, linkedList.delete(999)) # can delete tail, index 2 self.assertEqual(linkedList.to_list(), [1, 2, 3]) self.assertTrue(linkedList.delete(2)) self.assertEqual(linkedList.to_list(), [1, 2]) linkedList.push(3) self.assertEqual(linkedList.to_list(), [1, 2, 3]) # can delete middle, index 1 self.assertTrue(linkedList.delete(1)) self.assertEqual(linkedList.to_list(), [1, 3]) # can delete tail self.assertTrue(linkedList.delete(1)) self.assertEqual(linkedList.to_list(), [1]) # can not delete out bounds self.assertFalse(linkedList.delete(1)) self.assertEqual(linkedList.to_list(), [1]) # can delete index 0 self.assertTrue(linkedList.delete(0)) self.assertEqual(linkedList.to_list(), [])
import __init__ from singlyLinkedList import SinglyLinkedList from utils.ll_util import LinkedListUtil class Solution: def deleteNode(self, node): """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead """ nextnode = node.getNextNode() if not nextnode: del node return node.data = nextnode.data node.setNextNode(nextnode.getNextNode()) if __name__ == '__main__': print('Deleting a node form linked list') llist = SinglyLinkedList() LinkedListUtil().constructList(llist) llist.printList() dataToDelete = input("Enter Node data to delete:") node = llist.getNode(int(dataToDelete)) # llist.delNode(int(dataToDelete)) Solution().deleteNode(node) llist.printList()
else: odds.append(cur.value) pos += 1 cur = cur.next LL = SinglyLinkedList() while evens: LL.addAtTail(evens[0]) evens.pop(0) while odds: LL.addAtTail(odds[0]) odds.pop(0) return LL.head if __name__ == "__main__": LL = SinglyLinkedList() LL.addAtHead(1) LL.addAtTail(2) LL.addAtTail(3) LL.addAtTail(4) LL.addAtTail(5) head = getOddEvenList(LL.head) printList(head) LL_1 = SinglyLinkedList() LL_1.addAtHead(2) LL_1.addAtTail(1) LL_1.addAtTail(3) LL_1.addAtTail(5) LL_1.addAtTail(6) LL_1.addAtTail(4)
def setUp(self): self.list = SinglyLinkedList()
class LRU(object): """ 定义LRU类。 """ def __init__(self): ''' 初始化。 ''' self.__cache = SinglyLinkedList() self.__maxCacheSize = 5 def __len(self): ''' 获取缓存的大小。 ''' head = self.__cache.get_head() pos = 0 while head: head = head.next pos += 1 return pos def __add(self, data): ''' 将数据加入到缓存。 ''' head = self.__cache.get_head() if head: self.__cache.insert_to_head(data) else: self.__cache.set_head(self.__cache.create_node(data)) def __remove(self, prev, current): ''' 从缓存删除指定节点。 参数: prev:待删除节点的前一个节点地址。 current:待删除节点。 ''' prev.next = current.next def __is_exist(self, data): ''' 判断缓存中是否存在指定数据。 参数: data:指定数据,即访问的新数据。 返回: is_exist,旧节点的前一个节点,旧节点。 ''' head = self.__cache.get_head() prev = None is_exist = False while head: if head.data == data: is_exist = True break else: prev = head head = head.next return is_exist, prev, head def __is_full(self): ''' 判断缓存是否已满。 返回: True或False。 ''' if self.__len() + 1 <= self.__maxCacheSize: return False else: return True def update(self, data): ''' 根据LRU策略更新缓存。 参数: data:访问的新数据。 ''' #判断访问的新数据是否存在 is_exist, prev, oldnode = self.__is_exist(data) #存在,则删除旧节点,在插入新节点到头节点前 if is_exist: self.__remove(prev, oldnode) self.__add(data) #不存在 else: #缓存已满,则删除尾节点,在插入新节点到头节点前 if self.__is_full(): head = self.__cache.get_head() prev_tail = None tail = None while head: if head.next.next == None: prev_tail = head tail = head.next break else: head = head.next prev_tail.next = None self.__add(data) #缓存未满,则直接插入新节点到头节点前 else: self.__add(data) def print_all(self): ''' 打印缓存的所有数据。 ''' head = self.__cache.get_head() if head == None: print('当前链表没有数据!') return while head: print(str(head.data) + '->', end='') head = head.next print('\n')